How to show the real post date in a draft

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?

Read More

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.

Related posts

Leave a Reply

4 comments

  1. 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 );
    
  2. 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?

  3. 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.

  4. 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.