Though this isn’t quite the same as displaying your most popular posts in your sidebar, here is some code you can use to display the posts that have received the most comments.  You’ll want to place it in your sidebar where you want the code to be displayed.

Most Commented Posts Code

Go into your theme files and go to header.php file.   Somewhere within the </head> you’ll want to place the following code:

<?php most_popular_posts($no_posts = 5, $before = '<li>', $after = '</li>', $show_pass_post = false, $duration='') {
global $wpdb;
$request = "SELECT ID, post_title, COUNT($wpdb->comments.comment_post_ID) AS 'comment_count' FROM $wpdb->posts, $wpdb->comments";
$request .= " WHERE comment_approved = '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish'";
if(!$show_pass_post) $request .= " AND post_password =''";
if($duration !="") { $request .= " AND DATE_SUB(CURDATE(),INTERVAL ".$duration." DAY) < post_date ";
}
$request .= " GROUP BY $wpdb->comments.comment_post_ID ORDER BY comment_count DESC LIMIT $no_posts";
$posts = $wpdb->get_results($request);
$output = '';
if ($posts) {
foreach ($posts as $post) {
$post_title = stripslashes($post->post_title);
$comment_count = $post->comment_count;
$permalink = get_permalink($post->ID);
$output .= $before . '<a href="' . $permalink . '" title="' . $post_title.'">' . $post_title . '</a> (' . $comment_count.')' . $after;
}
} else {
$output .= $before . "None found" . $after;
}
echo $output;
} ?>

If you’d prefer to display more than 5 posts, you can change the $no_posts = 5 code to whatever number you want to display.

Okay, now, you need to figure out where you want to display these most commented posts (usually the sidebar) and place the following code:

<?php most_popular_posts(); ?>

And as is usually the case, if you want something easier, you can always go with a plugin such as the Popularity Contest plugin.

To see other code that we’ve featured, check out our WordPress Code page.

Kyle Eslick is WordPress enthusiast who took his passion for WordPress to the next level in 2007 by launching WPHacks.com as a place to share hacks, tutorials, etc. Follow Kyle on Twitter @KyleEslick!

  1. Mike Smith says:

    Nice little piece of code. Popularity Contest displays with more criteria then just most commented, so this piece of code will work well for someone who wants JUST the most commented. Is this/was this a plug-in or did you custom write this code for the article?

  2. Walt says:

    There’s also a plugin called “Most Commented” that does this…
    http://wordpress.org/extend/plugins/most-commented/

  3. Muhammad Siyab says:

    Walt, it’s quicker the code way, instead of the plugin. However for people who don’t like to mess with code, the plugin is perfect! 😀

  4. Chris says:

    shouldn’t the first line be
    <?php function most_popular_posts($no_posts = 5, $before…
    rather than
    <?php most_popular_posts($no_posts = 5, $before… ?

  5. steve says:

    Hi, I’ve used this code and really appreciate your posting it, but I’d like it to show only a list of posts with the most comments (that is, ‘type=comment’), not posts with the most comments, trackbacks and pingbacks taken together.
    Do you know how to modify the sql call to make it work this way?
    I think it would be a very helpful change. Hope to here from you.
    Thanks.

  6. Mark says:

    May I ask how to add the thumbnail to the output?

    is it possible?

    Thanks.

Trackbacks/Pingbacks »

  1. Artikel mit den meisten Kommentaren ohne Plugin anzeigen | mlogger says:
  2. Como mostrar los artículos más comentados en Wordpress sin usar plugins | Nosolocodigo says:
  3. Mostrar los articulos mas comentados en WordPress sin usar plugins says:
  4. Mostrar los Artículos más comentados en WordPress sin usar Plugins WordPress desGratis.COM says: