10

I need to pull out the post publish date in order to make the post post auto expire. The thing is I can't get the right publish date.

Here is my code:

 global $wpdb;

$post_ids = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_status ='publish'" );

foreach($post_ids as $id){

      $postdate = get_the_date("Y-m-d",$id ); //here is what I can figure out
       .......
      ......etc
}

When I echo the $postdate, it come out with a wrong date. Not the date that exist in the wp_posts table.

How can I get the date properly?

4 Answers 4

19

get_the_date must be used inside the Loop. For outside the loop use get_the_time.

$posts = get_posts(array('numberposts'=>-1)); //Get all published posts
foreach ($posts as $post){
    echo get_the_time('Y-m-d', $post->ID); //Echos date in Y-m-d format.
}

Consider replacing 'Y-m-d' in this example with get_option('date_format') as this will display the date as per your date format setting in wp-admin.

14
  • I tried get_the_time as well, but still give me the wrong date.
    – dev-jim
    Jun 19, 2012 at 12:15
  • 1
    And you give pass the post ID to it? get_the_time is the correct function to use here. Jun 19, 2012 at 12:20
  • 1
    Also - you shouldn't be using a custom SQL query here. Use get_posts and then if you want to extract just the IDs, use wp_list_pluck Jun 19, 2012 at 12:22
  • I am doing something to update the post_status based on the publish date, is there any other way to update the status? So far, SQL query seem to easiest way to me.
    – dev-jim
    Jun 19, 2012 at 14:04
  • 1
    @DavidHobs Fixed :) Jun 13, 2014 at 14:31
3

You can use get_post() or get_post_field() for this, both work outside the loop.

$post_object = get_post($id);
$post_date = date( 'F jS, Y', strtotime( $post_object->post_date ) );

A full list of values returned by get_post:

WP_Post Object
(
    [ID] =>
    [post_author] =>
    [post_date] => 
    [post_date_gmt] => 
    [post_content] => 
    [post_title] => 
    [post_excerpt] => 
    [post_status] =>
    [comment_status] =>
    [ping_status] => 
    [post_password] => 
    [post_name] =>
    [to_ping] => 
    [pinged] => 
    [post_modified] => 
    [post_modified_gmt] =>
    [post_content_filtered] => 
    [post_parent] => 
    [guid] => 
    [menu_order] =>
    [post_type] =>
    [post_mime_type] => 
    [comment_count] =>
    [filter] =>
)
1

Some Modern soultions

Solution One

<?php echo get_the_date('j F Y', get_the_ID()) ?>

Solution Two

<?php the_time(get_option('date_format')) ?>
-2

try like this

$getPosts = $wpdb->get_results( 
"
    SELECT ID, post_date,post_title
    FROM $wpdb->posts
    WHERE post_status = 'publish' 
        AND post_type = 'post'
        ORDER BY ID ASC
    "
);

foreach ( $getPosts as $myPost ) {
    $id = $myPost->post_date;
    echo $myPost->ID.' | '. $myPost->post_title.' | '. get_the_date("Y-m-d",$id ).'<br />';
}

edit

get_the_time Returns the time of the current post for use in PHP. It does not display the time. To display the time of a post, use the_time(). This tag must be used within The Loop.

get_the_date The get_the_date template tag retrieves the date the current $post was written. Unlike the_date() this tag will always return the date. Modify output with 'get_the_date' filter.

Am I missing something here?

4
  • Please merge your answers.
    – fuxia
    Jun 19, 2012 at 13:17
  • show me how to merge, please? Jun 19, 2012 at 13:29
  • Edit the first answer and copy the code from the second into it. Then delete the second.
    – fuxia
    Jun 19, 2012 at 13:32
  • I tried your codes, and I can get the value from get_the_time, not get_the_date.
    – dev-jim
    Jun 19, 2012 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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