another post with this same subject has been closed without allowing a single reply
All topics are automatically closed after 1 year.
Ah! So it wasn’t an official ruling, then. Thank you!
I too would like to learn anything known about this. Anyone who is experienced with drafting posts, feel free to contribute.
Ok–digging around in the source code for version 3.2.1, it looks like the draft date is changed to current_time() on line 2504 of wp-includes/post.php:
// If the post date is empty (due to having been new or a draft) and status is not 'draft' or 'pending', set date to now
if ( empty($post_date) || '0000-00-00 00:00:00' == $post_date )
$post_date = current_time('mysql');
I am not clear on why/how post status being draft means post_date is empty, since I know the post_date field is populated with the date I specified on import. I could hack wordpress here to prevent it from using the current time–but if post_date really is empty that might mean my new published post going live with a date of 00-00-0000. And anyhow I’d rather find a hook or some other way to do it cleanly.
I’ll keep looking.
It isn’t perfect, but here’s my workaround.
When I import a post as a draft, I give it a hidden custom field, _release_date, with value identical to post_date. Then I wrote a function to hook into draft_to_publish that compares _release_date to whatever post_date wordpress has since assigned, and if they’re different, reset post_date to what it should be:
//restore original post date when publishing drafts.
function preserve_draft_date($post)
{
$release_date = get_post_meta($post->ID,'_release_date',TRUE);
if ($release_date && $post->post_date != date('Y-m-d H:i:s',strtotime($release_date)) )
{
$post->edit_date = date('Y-m-d H:i:s',strtotime($release_date));
$post->post_date = date('Y-m-d H:i:s',strtotime($release_date));
wp_update_post($post);
}
}
add_action('draft_to_publish', 'preserve_draft_date', 10, 1);
Problems with this method:
- If I edit the draft through the Edit Post page before publishing, Publish still says “Immediately”, with the current date, rather than the date I want. This is confusing to my authors.
- Hooking this same code into ‘edit_post’, unfortunately, does nothing.