Support

Account

Home Forums General Issues Updating Post Values

Solved

Updating Post Values

  • This is probably me doing something fundamentally wrong but I just cant see why this code isn’t working

      // Update current post with new preview link
        
        $postInfo = array(
            'field_5cf1142a132e8' => $url,
            'ID' => $postID
        );
    
        $id = wp_update_post($postInfo);
    

    The code runs just after a CURL call that returns the $url – Ive verified everything there’s no errors returned from wp_update_post, no console errors etc, it seems to execute fine but the $url value doesn’t get updated on the post with $postID. Ive tried using the field key and field name but neither work.

    Please help an old guy who’s going mad !!

  • Your first error is that when updating post meta using wp_update_post() your meta values need to be in a nested array, example: (see https://developer.wordpress.org/reference/functions/wp_insert_post/)

    
    // Update current post with new preview link
        
        $postInfo = array(
            'meta_input' => array(
                'field_5cf1142a132e8' => $url
            ),
            'ID' => $postID
        );
    

    your second issue is that when updating fields in this manner you’d need to update 2 fields. One using the field name that holds the value and the second is the field key reference. The meta_key of this other field is "_{$field_name}" and the value of this field is the field key of the field you are updating.

  • Fantastic, as always, thank you so much for your help John – its working perfectly and my sanity has been (mostly) restored!

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

The topic ‘Updating Post Values’ is closed to new replies.