
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.
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.
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.
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/
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;
}}
?>
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 “;
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);
}
}
}}
?>
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
OR follow on Twitter
Twitter.com/DeDestruct
Comments
Nice one but very complicated way to do. But I think there’s an easier non-programmer friendly way to do this. We can always use query_posts or get_posts function.
To grab the latest 5 modified posts use this:
query_posts(’showposts=5&orderby=modified’);
//place your modified loop here
I think that’s pretty easier as compared to creating a plugin. Well that’s just my method.
This will be be helpfull i like to give users lots of extra info…with sliding pannels so they can find out what we are doing when were not publishing.
I do alot of re-editing.
Kristian’s last blog post… LA VIDA DESPUES DEL TRUENE? No es sólo llorar. - Powered by CommentLuv
Trackbacks
Leave a reply