WordPress custom field being lost on wp_update_post, with a twist

I’m using wp_update_post to programmatically add a title and tags to posts from the front end. I’m running into a hair-tearing issue with custom fields in the process: one of the two custom fields that are created and populated when the post is initially created has its value deleted, while the other is perfectly fine.

This is the part of the code I use to create the post in the first place:

Read More
// Set the post ID so that we know the post was created successfully
$post_id = wp_insert_post(
    array(
        'comment_status'=>  'closed',
        'ping_status'   =>  'closed',
        'post_author'   =>  $author_id,
        'post_name'     =>  $slug,
        'post_status'   =>  'publish',
        'post_type'     =>  'custom'
    )
);

// If the post was created properly
if($post_id) {

    // Add meta/custom field data to post
    add_post_meta($post_id, 'custom_random_id', $randomId);
    add_post_meta($post_id, 'viewcount', '1');

Then, this is the code I use to update the title and tags:

// Continue if untampered
if($new_hashed_value == $_POST['hash']) {

    $updatePost = array();
    $updatePost['ID'] = $post_id;
    $updatePost['post_title'] = $title;
    $updatePost['tags_input'] = $tags;

    if(wp_update_post($updatePost)) {

        totallyUnrelatedStuff();
    }

I understand from other posts that wp_update_post may delete values – but in this case, the custom field ‘custom_random_id‘ is always intact, and ‘viewcount‘ always has its value deleted.

I’ve tried altering it so that it goes:

if(wp_update_post($updatePost)) {

        update_post_meta($post_id, 'viewcount', '1');
    }

or even:

if(wp_update_post($updatePost)) {
        delete_post_meta($post_id, 'viewcount');                                    
        add_post_meta($post_id, 'viewcount', '1');
    }

But the viewcount field’s value continues to be erased.

Furthermore, just to throw another wrench at me,

if(wp_update_post($updatePost)) {
        delete_post_meta($post_id, 'viewcount');                                    
        add_post_meta($post_id, 'new_field', 'new_value');
    }

works perfectly.

Would anyone know what’s going on?

Thanks!

Related posts

Leave a Reply

1 comment

  1. I had a similar thing happen.

    wp_update_post calls the action save_post. Since you are using a custom post type you probably have a custom function to run on the save_post action to save your metadata.

    Problem is when you call wp_update_post your custom function for saving metadata is setting these values to blank because it can’t find the data its looking for (Typically in $_POST).

    You need to add some additional checks to see if your save_post action function should be runnning, in a way, test to see if it is being called from the Edit screen in WordPress, or from say a front-end form.

    In my case this solved the problem:

    function save_metadata($postid) {   
        global $post;  
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
        if ( !current_user_can( 'edit_page', $post->ID ) ) return false;
        if ( empty($post->ID) || get_post_type( $post->ID ) != 'post_type_here' ) return false;
        if ( !is_admin() ) return false;
    
        updateMyMetas();
    }