How to Display Most Commented Posts in WordPress Without a Plugin


When we wrote about how to display most commented posts in WordPress, we highlighted a plugin to make beginner’s life easier. However some of our users asked us if there was a way to display most commented posts without installing a plugin. In this article, we will share a code snippet that you can add to display most commented posts in WordPress without a plugin.

This is useful if you’re learning to build WordPress themes and do not want to use a plugin.

Please note, that this method is not suitable for beginners. If you do not feel confident about adding code, then you should checkout our guide on how to display most commented posts in WordPress by using a plugin. If you are looking for a way to display your most popular content, then checkout our list of the best popular posts plugins for WordPress.

Lets get started, first you need to add the following code to your theme or child theme’s functions.php file or a site-specific plugin.

    function wpb_most_commented_posts() {   // start output buffering  ob_start();  ?>  <ul class="most-commented">  <?php   // Run WP_Query  // change posts_per_page value to limit the number of posts  $query = new WP_Query('orderby=comment_count&posts_per_page=10');     //begin loop  while ($query->have_posts()) : $query->the_post(); ?>    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> <span class="wpb-comment-count"><?php comments_popup_link('No Comments;', '1 Comment', '% Comments'); ?></span></li>  <?php endwhile;   // end loop  ?>  </ul>  <?php    // Turn off output buffering   $output = ob_get_clean();     //Return output   return $output;   }  // Create shortcode  add_shortcode('wpb_most_commented', 'wpb_most_commented_posts');     //Enable shortcode execution in text widgets  add_filter('widget_text', 'do_shortcode');  

This code runs a database query and fetches 10 posts by comments count order. We have used output buffering, so that we can use the code to create a shortcode.

The last line enables shortcode execution in text widgets. Now in order to show the results, all you need to do is add [wpb_most_commented] shortcode in a text widget, or in any WordPress post or page.

To display post thumbnail next to post titles, you need to add this line just after <li> and post title.

  <?php the_post_thumbnail(array(40,40)); ?>  

The value used in array will define the custom size for the post thumbnail images. You can adjust it to meet your needs.

To style the output you can use .most-commented and .wpb-comment-count classes in your theme’s stylesheet. You can use this CSS to get started:

  .most-commented li {   border-bottom:1px solid #eee;   padding-bottom:3px;   }   .most-commented li :after {   clear:both;  }   .most-commented img {   padding:3px;  margin:3px;  float:left;  }  .wpb_comment_count a, .wpb_comment_count a:active, .wpb_comment_count a:visited, .wpb_comment_count a:hover {   color:#FFF;  }   

We hope this article helped you display most commented posts in WordPress witout installing a new plugin. Feel free to experiment with the code and css.

If you liked this article, then subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Google+ or Twitter.

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.