WordPress: Recently Updated Posts/Pages

Instead of manually showing what you’ve recently updated in WordPress, or having to widgetize your sidebar, you can use a code snippet.

recently updated

The best place to put this code is in your sidebar or footer, where you have other website statistics and such.

<?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("Recently Updated"); ?></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; ?>

Customising

The parts in bold are the ones you can edit.

You do not need to place the posts/pages in a bulleted list. They can be numbered if you like, or you can just use a <br /> after each post.

The comment number need not be in curly brackets; these can also be displayed how you like.

The number 5 is the number of posts that are displayed. Edit to your liking.

Stylesheet and CSS

You might need to change the margin and padding of your lists if you want to place the code in your sidebar. There might be too much padding (by default) so we can use a class to style it:

<h2>Recently Updated Posts/Pages</h2>
<ul class="list-recent-posts">
...
</ul>

Perhaps using something like this in our CSS:

.list-recent-posts {
	list-style-image:url('image URL');
	margin-left:0px;
	padding-left:9px;
}

Adjusting the padding on the left hand side will shift the list as far from the side as you wish.