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.

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?

share|improve this question

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer
    
I tried get_the_time as well, but still give me the wrong date. –  dev-jim Jun 19 '12 at 12:15
1  
And you give pass the post ID to it? get_the_time is the correct function to use here. –  Stephen Harris Jun 19 '12 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 –  Stephen Harris Jun 19 '12 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 '12 at 14:04
1  
@DavidHobs Fixed :) –  Stephen Harris Jun 13 at 14:31

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?

share|improve this answer
    
Please merge your answers. –  toscho Jun 19 '12 at 13:17
    
show me how to merge, please? –  Gembel Intelek Jun 19 '12 at 13:29
    
Edit the first answer and copy the code from the second into it. Then delete the second. –  toscho Jun 19 '12 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 '12 at 14:06

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.