How to get the latest posting time of archived pages in WordPress?

Is there a way to get the post time of the latest post under the WordPress archive page?

I would like to add a feature to the archive page to show the latest time of posts published under that category.

Any help, thanks in advance!

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

Query the posts ordered by date, restricted to 1 post to display, then get the date.

$args = array(
   'posts_per_page' => 1,
   'orderby' => 'date',
   'order' => 'DESC'
);

$lastpost = get_posts($args);
echo $lastpost[0] -> post_date;

Edited last line for proper display:

$lastpostdate = $lastpost[0] -> post_date;
echo '<div class="lastpostdate>"' . $lastpostdate . '</div>';

Or, if you already have the query with the default order, you can just get the last post’s date from it:

// assuming $myquery is your query

$lastpostdate =  $myquery -> posts[0] -> post_date;

Or, if you have not altered the global query, in archives just use:

$lastpostdate = $wp_query -> posts[0] -> post_date;


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
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x