How to get the post publish date outside the loop?

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.

Leave a Comment