Updating posts with $wpdb->update() and wp_update_post()
-
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?
- The topic ‘Updating posts with $wpdb->update() and wp_update_post()’ is closed to new replies.