Display most commented posts from a single category

It’s in human nature to socialize with other people and to listen to other’s opinions. Whether we like it or not, other people’s opinions matter to us. It is the fact that people would give advantage to a product which other folks (even complete strangers) have already approved, contrary to a product which people haven’t approved or even simply ignored.

Just for an example, people are more likely to buy a book for which other people have already said it was good than giving a chance to one no one ever picked up from the shelf. To make it even better, people don’t have to say that the book was good – it only has to be displayed separately in order to bring your attention, right?

The same goes for your posts; if a post is popular, i.e. has more comments than other posts, people will be more interested in them. It’s a phenomenon which skilled people in marketing know how to use “against us” and take more money from us.

Because of that it is a good idea to give your visitors a list of most popular posts on your site and make the list easily accessible to them. Your visitors will get easy access to the most interesting stuff on your site and you will get a click or two more by making a visitor interested. Maybe you can even interest them enough to post a new comment on already popular stuff.

OK; let’s stop with the talk and prepare for the actual work.

You can display most popular posts in many different ways. In this article, we’re about to show you how to create a list of your most popular posts from only one category and order them by comment count – the more comments it has, more popular the post is, right? To continue on the previous example, let’s say you have a “books” category and you want to list out the most commented posts from it.

Before you start dealing with the code, you should decide where you want your list to be displayed. If you have a location in mind, let’s start building towards the list:

  1. Open a file which can display the list (sidebar.php if you want the list on your sidebar, single.php if you want to include the list on bottom of every post for example, etc.)
  2. Copy and paste the following code:
  3. <?php
    $args=array(
    'cat' => 3,
    'orderby' => 'comment_count',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 6,
    );
    
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) { ?>
    <ul>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    <?php  endwhile; ?>
    </ul>
    <?php }
    
    wp_reset_query(); ?>
    
  4. Find the category ID which you want to display and change it on the 3rd line
  5. Change number of post that you want to list out on the 8th line
  6. Save changes

After you have made changes and saved them, you should be able to see your newly created list. If you haven’t changed how posts should be ordered, the post with the most comments in a category will appear on top of the list. The list will change dynamically according to number of comments in each post of selected category.

As you can see from the code, it is relatively easy to change some of the other parameters and tailor the list to your needs.

Enjoy your list of most commented posts and tell us how it’s working for you. Since you’re already working with post, you can highlight comments by specific user or author, show number of posts by any author or even forbid specific words to be used in comments.

If you’re interested into modifying comments, see all of our articles related to comments.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *