WordPress Quick Tip: Display Posts With The Most Comments
In this post we’re going to look at how you can easily display the posts on your site that have received the most comments. This can then be used as a section on your site where you can display your “Popular Posts”.
Having a section like this can be good for getting visitors on your site to stay around longer and read more of your content.
In this tip I’m going to be using the Zarina WordPress theme, but the code we’ll be using will work with any theme – we want to add the popular posts section just below the photos:

Add the following code at the bottom of your sidebar.php file:
<?php query_posts(array('orderby' => 'comment_count', 'showposts' =>10 )); ?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <ul> <li><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php echo $post->post_title; ?></a></li> </ul> <?php endwhile; endif; ?>
Save changes and then view your site – you should be able to see your posts with the most comments:

You may need to style the posts to make them look consistent with the rest of your site, but links to the most commented articles on your blog should be displayed.
You can use this code anywhere on your site – for instance, another useful place to present “popular posts” is at the bottom of your blog posts – this provides visitors with further options after they have finished reading the current post. To achieve this all you need to do is add the above code at the bottom of your single.php file and you should see the “most commented” posts appear.
I hope you found this useful – leave a comment below if you have any questions/suggestions related to this tip.

















June 2nd, 2010 at 5:38 pm
Don’t forget to reset the query using wp_reset_query();. Some problems may occur if you don’t do that.
June 3rd, 2010 at 4:20 am
Good point Slobodan – always worth including wp_reset_query(); if you’re using multiple queries.