How to display Posts with most Comments

comments_iconThis might be a known code for many theme developers, if you aren’t aware about this one. You can see some websites displaying popular posts, those are nothing but posts ordered with most number of comments. Adding it to your theme is quite simple, just add this code

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

The above code will display 10 posts with most number of comments in lists i.e between <li> </li> tags. I’m going off to sleep now, I’ll show you how to create a similar widget for sidebar later. Have a good night

Related posts:

  1. How to prevent spam comments using .htaccess file
  2. How to Insert content after each post in WordPress
  3. How to Display Technorati Stats on Your Blog
  4. MS DOS Tutorials – How to Create, Delete, Change, Rename Directory and Files
  5. How to make your WordPress Theme Widget Ready


The author is a small business owner himself specialising in Web Development. He runs Insight Studios which offers specialized Web Development, SEO Services and PPC Management for Small Business and SME thus ensuring growth and prosperity using the power of social media.

Share This Post

Related Articles

Leave a Reply

© 2012 MyGeekPal. All rights reserved. Site Admin · Entries RSS · Comments RSS
Designed by Insight Studios