Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
1  
What do you mean "the one I selected?" The post date field (and the functions that get it) are meant to retrieve the publication date of your post. If your post is still a draft, it won't have a publication date! –  EAMann Feb 28 '12 at 20:26
    
@EAMann I'm referring the date I saved in Wordpress's "Publish" metabox on the post edit screen. If you can modify and save a date on the edit screen of a draft (which you can), I think it's reasonable to expect that date would (or could) be reflected in the preview. Showing today's date makes sense if you haven't modified the date and it's still set on "Publish immediately". But if another date has been saved, replacing it with today's date seems counterintuitive. –  supertrue Feb 29 '12 at 1:36
    
So. Did you check out my answer? It's the easiest way to achieve what you asked for. –  jleander Jun 13 '13 at 11:57

4 Answers 4

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:

function date_dump_callback( $date, $d )
{
    echo '<pre>'; print_r( $date ); print_r( $d ); echo '</pre>';
    return $date;
}
add_filter( 'get_the_date', 'date_dump_callback', 20, 2 );
share|improve this answer
    
Ok, but is there a way to show the date I selected? –  supertrue Feb 23 '12 at 13:54
    
See update..... –  kaiser Feb 23 '12 at 14:04
    
Unfortunately the selected date typically won't match post_modified. Is there another way to get it? It's definitely getting saved somewhere, because it's remembered on the edit page. –  supertrue Feb 23 '12 at 14:50
    
See Update. .. . –  kaiser Feb 23 '12 at 15:05
    
The debug function is showing the same date (the current date/time, not the post date I selected). I can see in the database that the post date I selected (the one I want to show) is saved in the post_date column of the posts table. $post->post_date gives the same result as get_the_date(), though. –  supertrue Feb 24 '12 at 3:37

I'm not sure why it is modifying the data like that when displaying, but you can use

$post->post_date_gmt

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:

$date = date_i18n( $datef, strtotime( $post->post_date ) );

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:

<?php
/**
*@param string $datef (optional) to pass the format you want for the returned date string
*@return string
*/
function get_the_real_post_date($datef = 'M j, Y @ G:i') {

    global $post;

    if ( !empty( $timezone_string = get_option( 'timezone_string' ) ) )  {

        $timezone_object = timezone_open( $timezone_string );
        $datetime_object = date_create( $post->post_date_gmt );

        $offset_sec = round( timezone_offset_get( $timezone_object, $datetime_object ) );
        // if you want $offset_hrs = round( $offset_sec / 3600 );

        return date_i18n( $datef, strtotime( $post->post_date_gmt ) + $offset_sec );

    } elseif (!empty( $offset_hrs = get_option('gmt_offset') ) ) {

        // this option shows empty for me so I believe it's only used by WP pre 3.0
        // the option stores an integer value for the time offset in hours

        $offset_sec = $offset_hrs * 3600;
        return date_i18n( $datef, strtotime( $post->post_date_gmt ) + $offset_sec );

    } else {

        return;  // shouldn't happen but...

    }
}

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?

share|improve this answer
    
[After a year scouring the earth for answers] In my drafts, $post->post_date_gmt is just 0000-00-00 00:00:00. So that gets in the way of this working. I wish I could figure this one out! –  supertrue Jun 4 '13 at 15:08

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:

// function to get scheduled date for draft post
function unpublished_draft_date(){
  global $wpdb; global $post;
  $post_id = $post->ID;
  $draft_date_array = $wpdb->get_results( 'SELECT post_date FROM wp_posts WHERE ID = '.$post_id );
  $draft_date = $draft_date_array[0]->post_date;
  return $draft_date;
}

And this inside your post loop:

echo unpublished_draft_date();

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.

share|improve this answer

In my drafts, $post->post_date_gmt is just 0000-00-00 00:00:00 – supertrue Jun 4 at 15:08

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.

If you can modify and save a date on the edit screen of a draft (which you can) – supertrue Feb 29 '12 at 1:36

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.