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

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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *