How to Display Last Updated Posts in WordPress

Display Last Updated Posts in 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.

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

function WTips_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', 'WTips_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 last updated posts. Just use the code given below on the page where you want to display posts.

<?php

if (function_exists(WTips_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 last updated posts with this shortcode [lastupdated-posts-wt].

Get your free WordPress blog here

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

Advertisement

Tags: |

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.


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.

2020 UseThisTip | Part of Techlomedia Internet Pvt Ltd Developed By Deepanker