Display the Latest Modified Posts in WordPress

Display only posts that were modified last, and not necessarily posts that were published last.

Display the Latest Modified Posts in WordPress
Photo by Fikret tozak / Unsplash

In a previous post, I showed you how-to display the latest posts in WordPress. In this tutorial, I’m going to show you how-to display the latest modified posts in WordPress.

What’s the difference? This post displays the latest posts that have been modified, not just the newest posts. Why would you want this? Posts may change at any time, for any reason and you may want you visitors to know this. Think of a news website, where authors may post updates to news articles based on new information obtained or updates to live events.

I should point out that the code snippet may display posts that were last published.

Here is the full code snippets:

<?php
// Display the last modified posts in WordPress
$args = array( 'numberposts' => '5', 'orderby' => 'modified' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
	echo ''<a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" >' . $recent["post_title"] . ''</a>';
}
?>

In the above code snippet, we will display the last five (5) modified posts. Each link will be linked to the article and uses the article’s name as the link display name.

Let’s take a closer look on what you can change in the code snippet. Let’s first start with the array of arguments ($args) you can use; this is located on line three (3) of our code snippet:

'numberposts' => 10
'offset' => 0
'category' => 0
'orderby' => 'post_date'
'order' => 'DESC'
'include' => 
'exclude' => 
'meta_key' => 
'meta_value' =>
'post_type' => 'post'
'post_status' => 'draft, publish, future, pending, private'
'suppress_filters' => true

The arguments should be in the array and each should be separated by a comma. But you need to make sure that you include 'orderby' => 'modified' for the code snippet to display posts that have been modified. You can change the number of modified posts to be display by changing the number in the 'numberposts' => '' argument.

Lastly, let’s look at line six (6), where the output happens:

echo '<a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" >' . $recent["post_title"] . ''</a>';

You can modify anything inside echo '';. You can also add stuff before and after, but remember if you add anything before or after, each modified post will output what you added, as the output code is in a loop. If you don’t want the custom output to display for each post, put the custom output code before and / or after the code snippet.