Untitled UI logotext
Solutions
WebsitesEcommerceMobile AppsWeb AppsProduction Support & Maintenance
Our work
Company
About usBlogPodcastContact us
Book a free consultation

How do I iterate through recently modified posts in Wordpress?

Olivia Rhye

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.

Ready to start a project?

Book a free consultation
Untitled UI logotext
Our work
About us
Blog
Careers
Submit a ticket
Agency Partnerships
Contact
© 2024 fjorge. All rights reserved.
Privacy