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

This lists the posts in my custom taxonomy template page:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

How can I order those posts by comment count?

share|improve this question

WP Query has a comment_count parameter since 2.9.


query_posts(array('orderby' => 'comment_count', 'posts_per_page' => '10'));
if ( have_posts() ) : while ( have_posts() ) : the_post();
share|improve this answer
FYI: You have curly quotes in your code sample. – Mark Duncan Jul 22 '11 at 9:15
Strange, thanks, fixed. – Wyck Jul 22 '11 at 13:55
I tried this, but it listed posts that had nothing to do with the particular custom taxonomy. – Carson Jul 22 '11 at 19:30
That was not really your question. – Wyck Jul 23 '11 at 0:23
up vote 1 down vote accepted

A friend helped me with a solution that I could place in my functions.php file:

add_filter( 'pre_get_posts', 'my_pre_get_posts' );

function my_pre_get_posts( $query ) {

    if ( is_tax( 'locations' ) && empty( $query->query_vars['suppress_filters'] ) )
        $query->set( 'orderby', 'comment_count' );

    return $query;
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.