• I am creating a new post programatically from the front end successfully using wp_insert_post($new_post);

    However my problem arises when trying to access content from custom fields, in this case ACF fields. I cant seem to access the data until I have manually updated the post in the back end. Is there a way to programmatically update the post straight after creating it in exactly the same way as the update button on the back end? I have tried adding the below with no success:

    do_action( ‘acf/save_post’, $new_post_id );
    update_post_meta( $new_post_id );
    wp_update_post( $new_post_id );

    Any help appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • MK

    (@mkarimzada)

    Hi there,

    I’ve had a similar issue in the past. What I ended up doing was saving the wp_insert_post($args) as $post_id variable, then update the custom fields in your case acf with update_field($selector, $value, $post_id) right after inserting post(s). Remember you will need to create acf fields previously.

    The function look like this:

    function create_some_posts() {
      $args = array();
      $post_id = wp_insert_post($args);
      
      if( (get_field('acf_field')) ) {
        update_field('acf_field', 'value', $post_id);
      } 
    }

    I hope this helps!

    Thread Starter creativ3y3

    (@creativ3y3)

    Hi @mkarimzada, thanks for your thoughts, I will give that a try. I think you’re right it does seem to activate the field if updated at that point of inserting the post. Think on that basis I will need to loop through all the fields that are acf within the post and update them at this point. Since I’m looking to keep these fields blank or at “default state” I guess my values would have to reflect that. Hopefully by doing that it would “activate the fields” allowing me to update the fields at a later stage after some user interaction on the front end and with no requirement to save on the back end.

    Thanks again, this helps, I will give it a try.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘custom post type information missing when creating new post programmatically’ is closed to new replies.