How do I iterate through recently modified posts in WordPress?

December 8, 2011

I have a client who has a WordPress installation with thousands of pages. There are a couple things wrong with this, mainly relating to speed and editing practicality. It’s also an issue to find pages because they are ordered hierarchically. There are a few pages I modify fairly regularly and wanted a convenient way to find them. In order to do so, I needed to iterate through recently modified pages. Here’s what I came up with:

function recently_modified_dashboard_widget_function() {
	global $wpdb;
	$querystr = "
		SELECT $wpdb->posts.* 
		FROM $wpdb->posts
		WHERE $wpdb->posts.post_type = 'page'
		ORDER BY $wpdb->posts.post_modified DESC
		LIMIT 0,25

	echo "
    \n"; foreach($wpdb->get_results($querystr, OBJECT) as $post) { echo '
  • '.edit_post_link( str_replace( site_url(), '', get_permalink( $post->ID ) ), '', '', $post->ID )."
  • \n"; } echo "
\n"; }

The query gets the last 25 posts of type page ordered by post_modified. From there, a foreach loop through the result yields individual posts in object form. The I used the post’s permalink, stripping off the site_url() as the text for the edit_post_link() text. All is well. I’ll soon publish the plugin which adds the list to the admin dashboard.

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive