Display Most Commented Posts in WordPress (With and Without Plugin)

You don’t really need an additional plugin to display a list of your most commented blog posts since the output is generated via a standard WordPress query.

In this post, I will show you both ways of displaying the most commented posts in WordPress – with and without a plugin.

Display 5 Most Commented Posts With Titles

Simply copy and paste the code snippet into the appropriate theme template file, such as sidebar.php.

<ul> 
<? php $ result = $ wpdb-> get_results ("SELECT comment_count, ID, post_title FROM $ wpdb-> posts ORDER BY comment_count DESC LIMIT 0, 5"); 
foreach ($ result as $ post) { 
setup_postdata ($ post); 
$ postid = $ post-> ID; 
$ title = $ post-> post_title; 
$ commentcount = $ post-> comment_count; 
if ($ commentcount! = 0) {?> 
<li> (<? php echo $ commentcount?> comments): <a href = "<? php echo get_permalink ($ postid); ?> "Title =" <? Php echo $ title?> "> <? Php echo $ title?> </a> </ li> 
<? Php}}?> 
</ ul>

In line 2, replace “5” with a larger number if you wish to show more than five posts.

There are some titles that are too long to fit into a typical sidebar, so we’ll have to truncate them. You can display posts with shortened titles by using the following code snippet: 

<ul> 
<? php $ result = $ wpdb-> get_results ("SELECT comment_count, ID, post_title FROM $ wpdb-> posts ORDER BY comment_count DESC LIMIT 0, 5"); 
foreach ($ result as $ post) { 
setup_postdata ($ post); 
$ postid = $ post-> ID; 
$ title = $ post-> post_title; 
$ new_title = substr (trim ($ post-> post_title), 0, 47). ... '; 
$ commentcount = $ post-> comment_count; 
if ($ commentcount! = 0) { 
if (strlen ($ post-> post_title)> 47) {?> 
<li> (<? php echo $ commentcount?> comments): <a href = "<? php echo get_permalink ($ postid); ?> "Title =" <? Php echo $ title?> "> <? Php echo $ new_title?> </a> </ li> 
<? Php} else {?>
<li> (<? php echo $ commentcount?> comments): <a href = "<? php echo get_permalink ($ postid); ?> "Title =" <? Php echo $ title?> "> <? Php echo $ title?> </a> </ li> 
<? Php}}}?> 
</ ul>

As you can see in the above code snippet, “47” specifies the length of the truncated title. According to the width of your sidebar, you can adjust the number “47” so that the titles appear on one line. 

Display Most Commented Posts From A Specific Category

<?php
$args=array(
'cat' => 3,
'orderby' => 'comment_count',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
);

$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(); ?>

Line 3 should contain the unique ID of your targeted category. You can choose the number of posts displayed on each page on line 8.

Display Most Commented Posts With Thumbnails

<?php $popular = new WP_Query('orderby=comment_count&posts_per_page=10'); ?>
	<?php while ($popular->have_posts()) : $popular->the_post(); ?>	
	<?php $justanimage = get_post_meta($post->ID, 'Image', true); // get an image
		if ($justanimage) { ?>
	<img src="<?php echo get_post_meta($post->ID, "Image", true); ?>" alt="<?php the_title(); ?>" />
	<?php } else { ?>
	<img src="https://an-alternative-image.jpg" alt="" />
	<?php } ?>
	<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile; ?>

Display Most Commented Posts With Plugins

You can display most commented posts in WordPress with the following free plugin:

WordPress Popular Posts –  https://wordpress.org/plugins/wordpress-popular-posts/

WordPress Popular Posts plugin allows you to display the most popular posts on your site in the sidebar. Several criteria can be used to determine which items are most popular. Most of the time, the most popular articles are also the most visited. Therefore, you can order posts based on how many visitors click on them. 

In addition, the plugin allows you to sort popular posts by comment count. 

There are also other settings available in the plugin. If you want to display popular posts from a certain time period, you can do so. Additionally, you can exclude posts and pages based on their IDs. 

Video: How To Use WordPress Popular Posts Plugin

If you liked this post, please consider sharing it with your friends:

Pinterest

Leave a Comment