Show WordPress most popular posts based on comments

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 } } ?>

Related posts:

HTML5 page structure

HTML5 offers more powerful structure comparing to older versions of Html. There are many new …


Partner with Us

We are providing an exclusive partnership offer to individuals and companies who are interested in setting up their own web design & development company. For more information about how to partner with us, please contact us