wp_update_post based function works on existing posts, but not new posts

I have a function that will update the title of the post based on an ‘advanced custom field’.

It works on existing posts, but not when I create a new post. When I try and save the post and check the post listings, it isn’t there.

Any help would be greatly appreciated.

function my_post_title_updater( $post_id ) {

    if ( get_post_type() == 'equipment' ) {

        $my_post = array();
        $my_post['post_title'] = get_field( 'item_name', $post_id );

        wp_update_post( $my_post );

    }

}

// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 20);

1 Answer
1

I wasn’t adding the ID into the new array.

if ( get_post_type( $post_id ) == 'equipment' ) {

    $my_post = array();
    $my_post['ID'] = $post_id;
    $my_post['post_title'] = get_field( 'name', $post_id );

    wp_update_post( $my_post );

}

Leave a Comment