How to Display Last Updated Posts in WordPress

WordPress

Whenever you update a post in your WordPress blog, WordPress stores the update datetime of that post. So, you can use this data to display posts on the basis of the last update time. We have already posted how to display recent posts or display recent posts from a specific category in WordPress. In this post, we will see how to display recently updated posts. you can use it directly in your WordPress sidebar or use the code to make the widget.

In this article, I will show you how to show the last updated post using the code. If you are not interested in using the code, you can use the plugin to get a widget. Recent Posts Widget Extended is a good plugin to get that.

Display Last Updated Posts in WordPress

You can create a new function and shortcode for displaying the last updated posts in WordPress. You can either edit functions.php file or create a site-wide plugin.

Open functions.php of the theme and add the following code.

function TWG_lastupdated() {

$lastupdated = array(
'orderby' => 'modified',
'ignore_sticky_posts' => '1'
);

$wp_posts = new WP_Query( $lastupdated );
$counter = 1;
echo '<ul>';
while( $wp_posts->have_posts() && $counter < 5 ) : $wp_posts->the_post();
echo '<li><a href="' . get_permalink( $wp_posts->post->ID ) . '"> ' .get_the_title( $wp_posts->post->ID ) . '</a> ( '. get_the_modified_date() .') </li>';
$counter++;
endwhile; 
echo '</ul>';
wp_reset_postdata(); 
}
add_shortcode('lastupdated-posts-wt', 'TWG_lastupdated');

The above code is written only to display 5 posts. You can change the $counter limit to any value for increasing or decreasing the number of posts.

Best WordPress Hosting with Bluehost

Now you only need to use this function when you want to display the last updated posts. Just use the code given below on the page where you want to display posts.

<?php
if (function_exists(TWG_lastupdated)) :
WTips_lastupdated();
endif;
?>

If you have copied everything right, you will see the last updated posts.

You have already created a shortcode for this. So, you can also display the last updated posts with this shortcode [lastupdated-posts-wt].

If you want to show the modified date and time of any post within the loop, you can use the_modified_date() function.


Deepanker Verma is an experienced WordPress developer who has been working on WordPress for more than 12 years. On TheWPGuides, he writes about WordPress, WordPress development, and WordPress plugins.


Similar Articles

0 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

WordPress Maintenance Service

Struggling to manage your WordPress website? We can handle everythin so you can focus on running your business.

Contact Us
© 2022 The WP Guides Developed By Deepanker

The WordPress® trademark belongs to the WordPress Foundation. The WP Guides is not affiliated with or endorsed by the WordPress Foundation.