Home › Forums › General Issues › Add default Fields to Post programmatically › Reply To: Add default Fields to Post programmatically
I tried something else because I don’t want to create such a big wall of code for this task.
I have started with a empty post that is my template for this task. Then I get all the meta info with get_post_meta(template-post)
. Now I can add everything again to the new post that I created programmatically.
Because I also don’t want to have a empty post inside WordPress I exported the results from get_post_meta()
to a .json file. Now I use it as a template. This is my code for this task.
// Insert the post into the database.
$post = wp_insert_post( $new_post );
//add meta template to new post
$templateJson = json_decode( file_get_contents(get_theme_file_path('/json-templates/v1.json')));
foreach ($templateJson As $metaKey => $metaValue) {
$value = $metaValue[0];
add_post_meta($post, $metaKey, $value);
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.