How to: Display recently updated posts and pages

Ever wanted to be able to display posts and page that have been updated recently? If yes, here’s a nice recipe, provided by Corey, that will get the things done.

Paste this code anywhere you'd like to display a list of recently updated posts and pages. You can specify how much item to show by editing the value of the $howMany variable.

<?php
$today = current_time('mysql', 1);
$howMany = 5; //Number of posts you want to display
if ( $recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC LIMIT $howMany")):
?>

<h2><?php _e("Recent Updates"); ?></h2>
<ul>
<?php
	foreach ($recentposts as $post) {
		if ($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), $post->ID);
		echo "<li><a href='".get_permalink($post->ID)."'>";
		the_title();
		echo '</a></li>';
	}
?>
</ul>
<?php endif; ?>

Credits goes to Corey for this awesome code!

Related Posts

No related posts.

10 Responses

Nov 10 2008 21:16

Good one, thanks!

Nov 10 2008 21:21

You’re welcome! Corey did a nice job with that code :)

Nov 18 2008 13:40

Thank you so much! :)

Jan 11 2009 23:26

Thank your for the code. Is there a possibility to only show pages?

Jan 12 2009 00:43

@Johannes: I didn’t tested it, but if you add “AND post_type= ‘page’” to the WHERE clause, it should only get pages.

Jan 18 2009 01:40

@Jean-Baptiste: Works fine. Thanks!

Jan 29 2009 18:55

Sorry, but I’m new at this. Exactly where do I go in my WordPress setup to paste in this code? I’m using the Atahualpa Theme. Should this code go into the theme setup, or ? Thanks.

Sep 13 2009 14:22

Hi !!
This works great.

Is there a code to display the recently updated posts from specific categorie only?
Tnaks a lot !

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required