How to Display Most Recently Updated Post/Page in WordPress

Last updated
App Shah
Crunchify » WordPress Optimization and Tutorials » How to Display Most Recently Updated Post/Page in WordPress

WordPress Logo

Sometimes we need to show most recently updated Posts/Pages in WordPress Theme. Below simple function will help you on the same.

<?php
     $today = current_time('mysql', 1);
     $count = 5;
     if ( $recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC LIMIT $count")):
?>
<h2><?php _e("Recent Updates"); ?></h2>
<ul>
<?php
foreach ($recentposts as $post) {
     if ($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), $post->ID);
     echo "<li><a href='".get_permalink($post->ID)."'>";
     the_title();
     echo '</a></li>';
}
?>
</ul>
<?php endif; ?>

4 thoughts on “How to Display Most Recently Updated Post/Page in WordPress”

Leave a Comment