WordPress Snippets at WPcustoms

List popular posts based on comment count

place this code in your sidebar’s code to display 10 posts ranked by their comments count.


/**
 * Snippet Name: List popular posts based on comment count
 * Snippet URL: https://wpcustoms.net/snippets/list-popular-posts-based-on-comment-count/
 */
   $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) {
       echo '
  • '. $title .' (' . $commentcount .')
  • '; } }