WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
@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. – MikeSchinkel Sep 8 '10 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 '10 at 17:17
query_posts( array('orderby' => 'comment_count', 'order'=>'DESC') )

This should do it.

share|improve this answer
$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.

share|improve this answer

@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.

share|improve this answer

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 )
) );
share|improve this answer

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 } } ?>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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