Support

Account

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);
    }