WordPress: The wp_update_post Dates in Drafts

December 1st, 2009

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:

1
2
3
4
5
6
7
$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.

Related posts:

  1. Customize Posts Order in WordPress via Custom Fields
  2. Adding mod_rewrite Rules to .htaccess in WordPress
  3. Thickbox and jQuery in WordPress 2.8
  4. WordPress: Tag Cloud Based on Author Posts
  5. jQuery in WordPress: wp_enqueue_script

Tagged: ,
Shortlink: http://kovshenin.com/1649

Permalink or share:
  • Twitter
  • Digg
  • Facebook
  • del.icio.us
  • FriendFeed
  • Technorati
  • Google Bookmarks
  • LinkedIn
  • Ping.fm
  • Identi.ca
  • StumbleUpon
  • Print
  • email

4 Responses to “WordPress: The wp_update_post Dates in Drafts”

  1. Andy says:

    Hi, Thanks for the great tip. Had been wondering why I was not able to set the dates to future when I manually schedule to publish in the future. Before I got your solution, I found that the date gets updated when you call wp_update_post twice. I called them with a sleep(1) between them. No idea why it was updating when we do that. Thanks a lot again.

    Best.

Including pingbacks & trackbacks

  1. uberVU - social comments linked here on December 1, 2009 at 5:42 pm

Leave a Reply