How to get the post publish date outside the loop?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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] =>
)

Method 3

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')) ?>

Method 4

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?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments