Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

// 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!

share|improve this question
    
Where si the code for the unaltered wp_update_post? –  vancoder Apr 29 '13 at 16:19
    
By "erased" do you mean "set to 1"? –  s_ha_dum Apr 29 '13 at 16:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.