WordPress: Display Recently Updated Posts with Shortcode

wordpress-4Floating around the Internet is a piece of PHP code that is designed to display recently updated posts/pages in WordPress. For some reason or another, I was unable to get the code to work with the expected results. I attempted to convert the code for use with functions.php and shortcodes with little success. After submitting a request to experts-exchange for assistance, within moments, I had the raw code I needed to use shortcodes. However, the original PHP code still didn’t work for my WordPress installation. After some tweaks and a lot of additional bells and whistles, I had managed to piece together the code needed to only display recently updated posts. This final piece of shortcode is tested and works in WordPress version 2.9.2.


//begin - Recent Updates Shortcode --- Posts Only
//Inspiration: Display Recently Updated Posts/Pages available at http://wphacks.com/huge-compilation-of-wordpress-code/
//Thanks to jayarjo for assistance to convert to shortcode at http://www.experts-exchange.com/Web_Development/Blogs/WordPress/Q_25981209.html

add_shortcode('recent_updates', 'shortcode_recent_updates');
function shortcode_recent_updates($attrs, $content)
{
ob_start();
global $wpdb;
$today = current_time('mysql', 1);
$howMany = 50; //Number of posts you want to display
if ($recentposts = $wpdb->get_results("SELECT ID, post_title, post_modified FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_name NOT LIKE '%revision%' AND post_name NOT LIKE '%autosave%' 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);
}
/* If no post title exists one will be assigned to it. */
echo "<li><a href='".get_permalink($post->ID)."'>";
echo mysql2date('m/d/Y', $post->post_modified); //Source: http://stackoverflow.com/questions/1328644/format-dates-in-a-wordpress-plugin
echo "&nbsp;-&nbsp;";
echo $post->post_title;
echo '</a></li>';
}
?>
</ul>
<?php endif;
return ob_get_clean();
}

//end - Recent Updates Shortcode

An example of this in use is here: http://www.elvisrecords.us/updates/

code-example

Update (7/2/2026)
This version is untested and was re-imagined by AI when I fed it this 2011 code.  It supposedly fixed.

/**
 * Shortcode: [recent_updates limit="50"]
 * Displays recently modified published posts.
 */
add_shortcode( 'recent_updates', 'shortcode_recent_updates' );

function shortcode_recent_updates( $atts ) {
    $atts = shortcode_atts(
        [ 'limit' => 50 ],
        $atts,
        'recent_updates'
    );

    $limit = absint( $atts['limit'] );

    $query = new WP_Query( [
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'posts_per_page' => $limit,
        'orderby'        => 'modified',
        'order'          => 'DESC',
        'no_found_rows'  => true, // skip COUNT() for performance
    ] );

    if ( ! $query->have_posts() ) {
        return '';
    }

    $output = '

' . esc_html__( 'Recent Updates', 'your-textdomain' ) . '

    '; while ( $query->have_posts() ) { $query->the_post(); $title = get_the_title() ?: sprintf( esc_html__( 'Post #%s', 'your-textdomain' ), get_the_ID() ); $date = get_post_modified_time( 'm/d/Y', false, get_the_ID() ); $permalink = get_permalink(); $output .= sprintf( '
  • %s – %s
  • ', esc_url( $permalink ), esc_html( $date ), esc_html( $title ) ); } wp_reset_postdata(); $output .= '
'; return $output; }

Sources
Original Code: http://wphacks.com/huge-compilation-of-wordpress-code/
Shortcode Conversion: http://www.experts-exchange.com/Web_Development/Blogs/WordPress/Q_25981209.html
Code fragments: http://www.wprecipes.com/how-to-display-the-total-number-of-posts-on-your-wordpress-blog
Code fragments: http://stackoverflow.com/questions/1328644/format-dates-in-a-wordpress-plugin