wordpress most commented posts

Following code will display most commented posts on your blog. You can change the number of posts displayed by changing the limit value in the mysql query.
Add following function in your theme’s function.php file

<?php
function wp_most_commented_posts($count=5)
{
global $wpdb;
$result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM ".$wpdb->posts." ORDER BY comment_count DESC LIMIT 0 ,".$count);
$result_data="";
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { 
	$result_data.='<li><a href="'.get_permalink($postid).'" title="'.$title.'">'.$title.' ('.$commentcount.')</a></li>';
	}
	} 
echo $result_data;
}
?>

Display Commented Posts

To display most commented posts on your blog add following code inside your themes .php file where you want to display the most commented posts.

<ul id="toppost-list">
<?php
wp_most_commented_posts(7);
?>
</ul>

Above code will display max 7 most commented posts. Change this value if you want to change the number of posts displayed.

Leave a Reply

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