Get the most recently modified post date of most recently modified post
-
21-04-2021 - |
Question
I have Googled this, but there are always answers to get the modified date of a single post, which I know how to do.
What I'd like to do is get and display the most recent modified date of the most recently modified post.
In other words, if I have 100 posts on a site and someone modifies any one of them, I want to display that date elsewhere on the site.
As in "Most recent site update: March 25, 2018"
Solution
Try this:
$q = new WP_Query;
$posts = $q->query( array(
'posts_per_page' => 1,
'orderby' => 'post_modified', // Sorts by the date modified.
'order' => 'DESC', // Sorts in descending order.
'no_found_rows' => true,
) );
// Note that $posts could have more than one post, if your site have sticky
// posts. However, the first post, unless filtered by plugins, would always
// be the most recently modified/updated one.
if ( ! empty( $posts ) ) {
$post = $posts[0];
setup_postdata( $post );
?>
<p>Most recent site update: <?php the_modified_date( 'F d, Y' ); ?>.</p>
<?php
wp_reset_postdata();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange