Richard Castera

Application Developer/Designer
  • Home
  • About
  • Projects

WordPress – List your most popular Posts

This function below creates a list of links of the top commented posts on your WordPress blog.

<?php
function listPopularPosts() {
    global $wpdb;
    $strBuidler = '';
    $result = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
    foreach ($result as $post) {
	setup_postdata($post);
	$postId = $post->ID;
	$title = $post->post_title;
	$commentCount = $post->comment_count;
	if ($commentCount != 0) {
		$strBuidler .= '<li>';
		$strBuidler .= '<a href="' . get_permalink($postId) . '" title="' . $title . '">' . $title . '</a> ';
		$strBuidler .= '(' . $commentCount . ')';
		$strBuidler .= '</li>';
	}
    }
    return $strBuidler;
}
?>

Call the function in your sidebar of footer like this:

<h2><?php _e('Popular Posts'); ?></h2>
<ul>
	<?php echo(listPopularPosts()); ?>
</ul>
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Don't forget to Subscribe and Follow!

Be sure to subscribe to the feed and follow me on Twitter for more insights and resources!

9 Responses to “WordPress – List your most popular Posts”

  1. Comment #1 - Permalink
    Cory Lankin
    05/17/2009

    Awesome post :)

  2. Comment #2 - Permalink
    Loren
    05/19/2009

    This works well. How would you limit the output to the 5 or 10 most commented?

  3. Comment #3 - Permalink
    Richard
    05/19/2009

    Hello Loren,

    You can easily change the number listed by changing the value in the SQL Statement. So, below where it says “LIMIT 0 , 5″ at the end of the statement, you would change 5 to the number desired. :)

    $result = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
    
  4. Comment #4 - Permalink
    chaz
    06/21/2009

    It seems as though that if you had a lot of posts that your script would have to query the whole DB to find just the top 5 most actively discussed.

    1. Wouldnt it be better to change the query to limit to the last 60 days so that not only would you only have the more relevant articles but also so you dont have to query the whole DB? Or would you still have to query the db anyway?

    2. Ideally, it would be nice to set this up to rebuild as a cron job once per day and just include what is built by the daily job… no? Do wordpress offer anything like that? For instance, why build the category list every single time the page is hit when mine hasn’t changed in the last 6 months.

  5. Comment #5 - Permalink
    chaz
    06/22/2009

    What if you want to display them on the main page in the loop instead of the sidebar? It seems as though this only works in the disbar.

  6. Comment #6 - Permalink
    Richard
    06/28/2009

    Hello Chaz,

    This works on the home/main page as well.

  7. Comment #7 - Permalink
    Richard
    06/28/2009

    Hello Chaz,

    You are correct in stating that it queries the whole database. If you wanted to limit so that it queries a specified date range, that’s possible as well. Cheers!

  8. Comment #8 - Permalink
    david knight
    06/29/2009

    hi Richard
    Great code, thanks for that. One question if thats ok. How would you add more details to the list? Id love to be able to add more of the content, perhaps even categories. Is this possible? Thanks again for the code!

  9. Comment #9 - Permalink
    Richard
    07/04/2009

    Hey David,

    Thanks for your comments! You can definitely add more content. You just need to change the SQL Query. For example, the query below will pull the content for the post as well!

    $result = $wpdb->get_results("SELECT comment_count, ID, post_title, post_content FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
    

It's Your turn

Click here to cancel reply.
You

CAPTCHA Image
CAPTCHA Audio
Refresh Image



About The Author

Richard is a Web Developer currently working for SankyNet. He is available for freelance work. Visit Shifting Ideas for inquiries. Thanks!
  • Premium Email Templates

    Social Profiles

  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • Google
  • GitHub

    Last Tweet

  • Beware the heat-seeking Nerf machine-gun coming to a cubicle near you http://om.ly/svHF #fb
  • Follow me
  • Archives

    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • September 2008
    • August 2008
    • July 2008
    • June 2008
  • Categories

    • AJAX
    • Apache
    • Books
    • Drupal
    • Ecommerce
    • Flash
    • Google
    • Javascript
    • jQuery
    • Magento
    • Marketing
    • Mootools
    • My Thoughts
    • MySQL
    • News
    • Photoshop
    • PHP
    • Prototype
    • SEO
    • Web Resources
    • Wordpress
  • My Favorites

    • 37 Signals
    • Andrew Warner
    • Chris Coyier
    • Chris Shiflett
    • Collis Ta'eed
    • Development Seed
    • Drupal
    • HubSpot
    • James Padolsey
    • Joel On Software
    • John Resig
    • jQuery
    • Kevin Rose
    • Life Hacker
    • Magento
    • Mashable
    • Matt Cutts
    • Matt Ryan
    • MySQL
    • NetTuts
    • Noupe
    • Photoshop Tutorials
    • PHP
    • Smashing Magazine
    • Tech Crunch
    • WoorkUp
    • Wordpress

2010 © Copyright. Richard Castera - All Rights Reserved.

Top