Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Display Most Commented Posts in WordPress Without a Plugin

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
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
<ul class="most-commented">
// 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

// 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; 
.most-commented li :after { 
.most-commented img { 
.wpb_comment_count a, .wpb_comment_count a:active, .wpb_comment_count a:visited, .wpb_comment_count a:hover { 

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.

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. Lukasz says:

    How i can set this to show post only from last 7 days?

  2. Alabi says:

    Thank you for this saved me sometime

  3. Aidan says:

    Awesome, thank you.

  4. Niloy Sarker says:


    Thanks Mr. Syed Balkhi For This Nice tutorial.

    some days ago my client want this feature on his Website and I used a plugin.
    and now i can use this code on her website.
    Thanks again :)

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.