Home > Scripts, Tips and Tricks, Wordpress > WordPress: Display Recently Updated Posts with Shortcode

WordPress: Display Recently Updated Posts with Shortcode

April 22nd, 2010

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

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

Comments are closed.