How-To: Display Recently Updated Posts in Wordpress

Get WordPress to show the last 10 posts modified.

Asked by Jamie a Student from Florida

Is there a plugin or hack to get WordPress to show the last 10 posts I’ve modified.

I often update my old posts with new information, but I’d like to have a list of the ones I’ve just recently updated.

I dont care about recent posts, but recently UPDATED ones.

Theres plugins to do this, But I’m going to show you how to code this yourself.

There is a pre-defined Template Tag in wordpress named wp_get_archives.

By default it only shows yearly, monthly (Default), daily , weekly or postbypost(date published).

We will base our hack on this function and extend it to sort posts by last updated.

Writing a Plugin

This will be a quick and easy plugin, great for newbies who have yet to write heir first plugin.
All you need to follow along is a text editor, notepad will do fine.

Follow along or skip to the end for the finished plugin.

Step 1

Start a new file in your text editor and add the following lines. These define your plugin to wordpress.
You can replace my name and urls with your own.

<?php
/*
Plugin Name: Last Updated Posts
Plugin URI: http://www.dedestruct.com/plugins/
Description: Returns a list of the most recently updated posts.
Version: 0.1
Author: Ayush Saran [ the Insomniac @ dedestruct.com ]
Author URI: http://www.ayushsaran.com/
*/

function dd_recently_updated($args = ”) {

}

?>

Save the file as last-updated.php in your plugins folder. usually /wp-content/plugins/

Step 2

From the base of your wordpress install find wp-includes/general-template.php.
This file contains the definition for wp_get_archives which will serve as our reference.

Open the file and ~line 350 you will see function wp_get_archives($args = ”) {

Copy the first 15 lines of the function into your blank plugin file.

You can delete the text ‘type’ => ‘monthly’, since we will only be offering the type of updated.

You should end up with a file that looks like this.
<?php
/*
Plugin Name: Last Updated Posts
Plugin URI: http://www.dedestruct.com/plugins/
Description: Returns a list of the most recently updated posts.
Version: 0.1
Author: Ayush Saran [ the Insomniac @ dedestruct.com ]
Author URI: http://www.ayushsaran.com/
*/

function dd_recently_updated($args = ”) {
global $wpdb, $wp_locale;

$defaults = array(
‘limit’ => ‘5′, ‘format’ => ‘html’, ‘before’ => ”,
‘after’ => ”, ’show_post_count’ => false
);

$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );

if ( ” != $limit ) {
$limit = (int) $limit;
$limit = ‘ LIMIT ‘.$limit;
}

}

?>

Step 3 - Modifying The Database Query

Back to the wp-includes/general-template.php.

Examine the last if..else statement.
$arcresults = $wpdb->get_results(”SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit”);

Here $orderby is defined as “post_date DESC “. This will fetch the posts and order them by the date published. We want to use the date modified as the condition for the sorting.

If you look at the database created by wordpress you will see that there is a column for post_modified that saves the date and time that any post was last modified.

We willl update the orderby to read $orderby = “post_modified DESC “;

Step 4 - Adding the new conditions

Copy over the filters line 392 -394 . And copy one of the if..else conditions to the new file, preferably postbypost.

Remove the IF conditions and the OR(||) statements. This may get confusing so refer to the code here for what you should have remaining.

The Finished Plugin
<?php
/*
Plugin Name: Last Updated Posts
Plugin URI: http://www.dedestruct.com/plugins/
Description: Returns a list of the most recently updated posts.
Version: 0.1
Author: Ayush Saran [ the Insomniac @ dedestruct.com ]
Author URI: http://www.ayushsaran.com/
*/

function dd_recently_updated($args = ”) {
global $wpdb, $wp_locale;

$defaults = array(
‘limit’ => ‘5′, ‘format’ => ‘html’, ‘before’ => ”,
‘after’ => ”, ’show_post_count’ => false
);

$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );

if ( ” != $limit ) {
$limit = (int) $limit;
$limit = ‘ LIMIT ‘.$limit;
}

//filters
$where = apply_filters(’getarchives_where’, “WHERE post_type = ‘post’ AND post_status = ‘publish’”, $r );
$join = apply_filters(’getarchives_join’, “”, $r);

$orderby = “post_modified DESC “;
$arcresults = $wpdb->get_results(”SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit”);
if ( $arcresults ) {
foreach ( $arcresults as $arcresult ) {
if ( $arcresult->post_date != ‘0000-00-00 00:00:00′ ) {
$url = get_permalink($arcresult);
$arc_title = $arcresult->post_title;
if ( $arc_title )
$text = strip_tags(apply_filters(’the_title’, $arc_title));
else
$text = $arcresult->ID;
echo get_archives_link($url, $text, $format, $before, $after);
}
}
}

}

?>

Step 5 - Final Steps

Log into your admin panel and activate the new plugin from the Plugins section.

You can now get the most recently updated posts by calling the dd_recently_updated function

Usage
<ul><?php dd_recently_updated(’limit=3′); ?></ul>

The default output is a list… i.e. wrapped in <li> tags
The default limit is 5, you can change that to reflect your needs