Wordpress recently updated pages or posts

Here is some code I hacked together to display a list of recently updated pages and posts on a Wordpress site:


<?php
$today = current_time('mysql', 1);
$howMany = 18;
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; ?>

The variable $howMany holds the number of items to display in the list. I used some code I found in a Wordpress theme I am using on some website, and modified it to help someone in need of this specific solution.

Update 11/08/2008: I just added this code to the bottom of this website and configured it to show only five items.

Update 02/22/2010:

Including post excerpts

A commenter below asks, “Is it possible to combine the_excerpt(); with the code you provided?” The function you are naming is only useful inside “the loop,” but yes, including excerpts is easy. Try this:


<?php
$today = current_time('mysql', 1);
$howMany = 18;
if ( $recentposts = $wpdb->get_results("SELECT ID, post_title, post_excerpt 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><p>' . $post->post_excerpt . '</p></li>';
}
?>
</ul>
<?php endif; ?>

11 Comments so far

  1. [...] goes to Corey for this awesome [...]

  2. Philip on September 4th, 2009

    Oh sweet … Ive been trying to do this for a while … Thanks for posting it have used it on my website.

  3. COMPUTELOGY on November 15th, 2009

    Hi,
    Is there any way to have such code for blogspot?
    Regards,

  4. Stupid WordPress Tricks | Serita on December 3rd, 2009

    [...] Source: Corey [...]

  5. [...] Source: Corey [...]

  6. [...] Source: Corey [...]

  7. Knut on February 22nd, 2010

    Great tutorial :)

    One question; Is it possible to combine the_excerpt(); with the code you provided?

    What I mean is that I want to display a list of the recently updated post/pages with the excerpt.

    Hope you can help me :)

    Best Regards
    Knut

  8. Corey on February 22nd, 2010

    I have added some code above to include post excerpts. Let me know how it works out for you.

  9. Knut on February 23rd, 2010

    Hi Corey,

    The new code that includes excerpts works perfectly. Thanks you so much!

    Now I have a new challenge for you; What would the code look like, if I want it to gather the 55 first words from a page / post?

    Hope you can solve this one ;)

    Best Regards
    Knut

  10. [...] Source: Corey [...]

  11. [...] Source: Corey [...]

Leave a reply

 

Thanks for reading!

You like? Subscribe to my rss feed
or

Sign up for email updates:

Some Recent Updates