How to Display Most Commented Posts in WordPress

0
1756

wordpress-most-commented-postsIn WordPress to display the most commented posts there are many plugins to do this. Using these codes can help accelerate your WordPress site and then display them on the page anywhere easily.

Method 1 : Using the loop
Allows you to use the WordPress loop to retrieve the articles that have the most comments . Copy the following code into your template to do this:

<ul>
<?php
$most_commented = array('posts_per_page' => 10,
'orderby' => 'comment_count',
'order' => 'DESC');
?>
<?php query_posts($most_commented); ?>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
</ul>


Method 2 : Using get_posts

This is my favorite method, its simple, compact and easy to edit:

<ul>
<?php
$most_commented = array(
'posts_per_page' => 10,
'orderby' => 'comment_count',
'order' => 'DESC'
);
$recentposts = get_posts($most_commented);
foreach ($recentposts as $post) : setup_postdata($post);?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>


Method 3 : Display most commented posts by category

With this method you”re only need to add a category_name argument .

<?php
$most_commented = array(
'posts_per_page' => 10,
'orderby' => 'comment_count',
'order' => 'DESC',
'category_name' => 'Category Name' 
// instead Category Name by your real category name
);
?>

Note that the above code is only suitable for WordPress 2.9 or higher , if you are using an older version you should update to the latest version before applying these codes . Good Luck !

LEAVE A REPLY

Please enter your comment!
Please enter your name here