duplicate posts when trying to update a post using the wp_insert_post.

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?

1 Answer
1

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.

Leave a Comment