Home Forums ACF PRO How to creare a post programatically?


How to creare a post programatically?

  • I have a custom post type, let’s say ‘Book’, and I would like to creare it from scratch using API, including setting all the custom ACF fields associated with the Book type.

    Which is the right approach?

  • Could do something like:

    *  Create a new post and add field data
    *  - if the post does not already contain a "reference" to the field object, you must use the field_key instead of the field_name.
    // Create post object
    $my_post = array(
     'post_title' => 'My post',
     'post_content' => 'This is my post.',
     'post_status' => 'publish',
     'post_author' => 1
    // Insert the post into the database
    $post_id = wp_insert_post( $my_post );
    // Add field value
    update_field( "field_5039a99716d1d", "I am a value!", $post_id );

    Snippet from:

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.