When looking at a draft post, get_the_date()
only seems to return today’s date, not the post date, even if I have selected another date on the post edit screen.
Is there a way to show the “actual” post date, the one I selected?
I can see in the MySQL database that it’s saved in the post_date column. But, like get_the_date, $post->post_date
doesn’t return it. If the post is a draft, it shows the current date and time.
Yes, as you – so far – have no publish date.
You could use
$post->post_modified
, which will always be the date of the latest modification to the post data.Debug:
Try hooking into the filter and dump both vars:
I’m not sure why it is modifying the data like that when displaying, but you can use
This will return the scheduled post date the same as it is in the DB except it’s in GMT time format, so you might need to convert the time to your local time zone first (this blog post may help). Otherwise, you should be able to use it as is if you’re just using the date & not the time but it depends on what you’re doing with it.
Edit 2/29/12:
I wanted to elaborate on my answer to make it more complete and give you something you can actually use.
You’re right, the post date is being stored in the
post_date
field in the database.For example, wordpress uses this line of code in
wp-admin/includes/meta-boxes.php
to set the variable that is used to display the post’s date for drafts scheduled for the future:However, when using the same code for display on the front end, it returns the current time like you said. I think we can conclude that the
$post
object data is being prepared differently for the front end.Anyways, it is possible to output the same scheduled date that you have set in the admin.
Because it seems like we can’t use
$post->post_date
, we can use$post->post_date_gmt
like I said before – the only downside is that your timezone probably isn’t the same as GMT. So all you need to do is pull the GMT value and convert it to your timezone.You can add this function to your
functions.php
and call it wherever you want:Of course you could also change the default time format that is defined in the parameter definition if there is a particular format you would use the most.
Let me know if this works for you. I’m curious, what are you using the date for?
You can output the scheduled date for a draft post by reading the database using wpdb class through the global $wpdb variable provided by Worpdress (see http://codex.wordpress.org/Class_Reference/wpdb ).
Add this to your functions.php:
And this inside your post loop:
I provided just the basic code for getting the date you wanted. It’s easy to form the date if you need and input a post id to the function if you don’t want to use current post id or you want to use the function outside the loop.
Please comment below if you need more specific instructions.
There is no way to edit the publish date of a draft that has not yet been published. The value for post_date_gmt will be 0000-00-00 00:00:00 until the post is published.
Make sure the post has been published at least once. Visibility may be set to private if you do not want it published publicly. Once it is published, you may edit the date and it should save that as the value for post_date and post_date_gmt indefinitely unless you change it again. The post_modified and post_modified_gmt values will continue to change any time you modify the post.
I have tested this and post_date_gmt will be set to the current date and time upon publishing the post with visibility set to private. You may also edit the date at this point and it should be updated.
No, you can’t modify and save a date on the edit screen of a draft. Prior to publishing a draft, this same area of the “Publish” metabox on the post editor is used for scheduling a date for the post to be published in the future. Of course you will not be able to set a past date or time for scheduling and the future date probably won’t be set for post_date_gmt until it is actually published.