Home » Labs » Show WordPress most popular posts based on comments  

Show WordPress most popular posts based on comments


Filed under Labs

If you want to show most popular posts of your WordPress blog based on comments, then bellow function is useful. Just copy and paste it in you template where you want to show popular posts.

 
<?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 $post) {
      setup_postdata($post);
      $postid = $post->ID;
      $title = $post->post_title;
      $commentcount = $post->comment_count;
      if ($commentcount != 0) { ?>
          <li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
          <?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
<?php } } ?>

Posts you may like: