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.

On an empty wordpress database, I insert one sample post using the wp_insert_post.

Since it's an empty database, the first post I add ends up with id 1. And I check that in the wp_posts, everything looks right ( well for some reason post_name is blank but I do not want to focus on that now ).

The question is but when I want to update that post ( again using the wp_insert_post), instead of updating the early record, I end up getting post id 2!

If you ask how I update the record with the wp_insert_post, I simply set the ID to 1 when I build my $data array for the wp_insert_post.

The codex say that when you supply the 'ID' array element to the wp_insert_post's $data array, it simply updates that post with that ID.

Well, in my cases, It does not.

I end up getting the post id 2. Post ID 2's author goes as 0 (post id's author was 1 ), and post id 2's post_name is 1-revision ( post id 1s post_name was blank as I said above)

Any idea of why this could be happening?

Or should I simply forget the idea of using wp_insert_post for post updates?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You should insert posts with wp_insert_post and update posts with wp_update_post. wp_update_post will add a revision to the database and update the published/draft post data.

share|improve this answer
    
Thank you. But, I think you meant "wp_insert_post will add a revision..." NOT wp_update_post. –  Average Joe Jun 5 '12 at 4:07
    
No I pull that back. you are right, wp_update_post also does the same thing, it too ads a revision. For a moment, I had thought only wp_insert does that revision thing. But all updates be it thru wp_insert_post or wp_update_post adds the revision post. And there is only one way to get rid of that, which is thru wp-config.php –  Average Joe Jun 5 '12 at 4:38
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.