I would like to be able to display a list of posts on a page depending on when and how many comments they've got. Something similar to how it is here on stackexchange.

To be more specific, I want:

  • A page with the most commented posts.
  • A page with the most recently commented posts.
  • A page with all the uncommented posts.

Help with this would be very appreciated, thanks in advance.

  • 1
    @Patrik: Where and how do you want to display them? As sticky posts, in a sidebar widget, or other? More details will help us answer. Sep 8, 2010 at 17:12
  • As a page with a list of blog posts. Just like a blog but instead of having the latest post show up first, the 2nd latest post after that etc, they are displayed depending on the conditions mentioned above. I hope you understand what I mean, english is not my primary language :)
    – Patrik
    Sep 8, 2010 at 17:17

5 Answers 5

query_posts( array('orderby' => 'comment_count', 'order'=>'DESC') )

This should do it.

$query = WP_Query;

foreach ($query['posts'] as $post){
  $count = $post['comment_count'];
  while (is_set($comment_sort[$count]){

$sorted = ksort($comment_sort);
$sorted = array_reverse($sorted);

this should give you an array of post ID's in decending order of comment counts.


@Patrik - I had a similar problem: In a WPMu installation, show the latest posts in post-order on the main blog. The easiest solution for me was to create a new table that holds the data for new blog posts (new posts are inserted via a plugin), and query that table for the main page. You can do the same, and just edit the SQL query to return based on most comments, etc.


Posts with recent comments:

$comments = get_comments( array(
    'status' => 'approve',
    'number' => 10
) );

$include = array();

foreach ( $comments as $comment )
    $include[] = $comment->comment_post_ID;

query_posts( array(
    'post__in' => array_unique( $include )
) );

Code snippet for displaying posts with most comments:

$result = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10"); // NUMBER OF POSTS
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); ?>"><?php echo $title ?></a></li>

<?php } } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.