Home Forums General Issues Create/update post title from ACF fields Reply To: Create/update post title from ACF fields

  • Elliot,

    This may seem like a no brainer but to us coming up in skill levels, this was one of those that I struggled with for too long a time. I share this because I would hope this detail might gain more visibility for the sake of us who are learning.

    This bit here:

        $my_post = array();
        $my_post['ID'] = $post_id;

    is necessary for when you call:

        // Update the post into the database
        wp_update_post( $my_post );

    It’s not mentioned in the context of : Link:

    And although it’s more a WordPress related function than a ACF one, it’s crucial that the array handed to wp_update_post() includes the $my_post['ID'] = $post_id; bit in the array passed to it.

    Wordpress documentation the description in it’s examples:

    Parameters #Parameters
    (array|object) (Optional) Post data. Arrays are expected to be escaped, objects are not. Default array.

    Default value: array()

    Doesn’t mention the necessity to pass the $post_id as: $myPostArray[‘ID’] assigned directly as that in that way. My mistake was to try and assign in this way:

    $myPostArray = array('post_id' => $post_id,
    'post_title' => $newTitle);

    This will fail with an error saying that the ID passed didn’t match.

    I spent no small amount of time figuring this out and the code example above is what made it make clear sense to me.

    It would be really great if this possible mistake was part of the documentation because it was really easy to make without the example above. It took me a good while to find this 😛