WordPress: The wp_update_post Dates in Drafts

This one’s pretty tricky. If you’ve ever tried to update a post using the wp_update_post function with drafts or pending posts, you might have noticed that the post_date argument is ignored, instead the current date/time comes up. The post_date argument works only for published posts or during the publishing process. I looked into the WordPress core code, the wp_update_post function has an additional argument called edit_date, which is not mentioned in the codex, so if you’re trying to update an existing post which is a draft or a pending one, use the following method:

$post_data = array(
	"ID" => 123,
	"post_date" => $post_date,
	"edit_date" => true
);

$post_id = wp_update_post($post_data);

This will update the post 123 and set it’s time to $post_date (which is in format Y-d-m H:i:s if you know what I mean ;). The other possible parameters for wp_update_post are described in the docs for wp_insert_post in the Codex.

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.

6 comments