• Chris

    (@comradeseidl)


    Hi.

    After updating to 3.5.1 I have encountered some strange behavior in WordPress that I can’t explain. Let me give an example.

    I use a custom post type, “my_post.” Previous to updating to 3.5.1, I used this code to update this post type’s status:

    $result = $wpdb->update(
    	$wpdb->posts,
    	array( 'post_status' => $new_status ),
    	array( 'ID' => $id )
    );

    This no longer updates the row in the database, but if I check $result, it is equal to 1, indicating that 1 row was affected by $wpdb->update(). So I decided to check what query was being sent to the database by adding this line right below the above code:

    exit( var_dump( $wpdb->last_query ) );

    Sure enough, this is what was returned to me (where 11111 is just an example ID):

    UPDATE wp_posts SET post_status = 'new_status' WHERE ID = 11111

    However, once I checked the database, exiting immediately after using the $wpdb->update() function actually resulted in the database row being updated. I removed the exit() code and tried again using the same code, just to make sure I was observing this correctly. Without the exit() code, no update was made to the database even though $result still returned 1.

    So then I gave up and decided to use this for updating my custom posts until I understand why $wpdb->update() is no longer working for me:

    $my_post = array();
    $my_post['ID'] = $id;
    $my_post['post_status'] = $new_status;
    wp_update_post($my_post);

    This solution works, but I want to know why $wpdb->update() is no longer working. Does anyone have any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • I also having the similar problem. When I try to change post status from “publish” to “draft” it duplicates the post and the new one is a “draft”. I need to change the status of the existing post instead.

    Please advice me where I have been wrong or is there anyway I can do that.

    Here I’m pasting the code I’ve used.

    $my_post = array();
    $my_post['ID'] = $id;
    $my_post['post_status'] = 'draft';
    wp_update_post($my_post);

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    I have been spending hours on google to find a solution for this. Please help me if any one know this.

    Thread Starter Chris

    (@comradeseidl)

    Hi ajith2011, please don’t hijack this thread. Start your own, please, as your question is entirely different than my own. It is bad practice to post your own question in an unrelated or tangentially related thread, and you will likely not receive a proper answer doing so.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Updating posts with $wpdb->update() and wp_update_post()’ is closed to new replies.