3

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?

0

2 Answers 2

6

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;
}
3

WP Query has a comment_count parameter since 2.9.

So:

query_posts(array('orderby' => 'comment_count', 'posts_per_page' => '10'));
if ( have_posts() ) : while ( have_posts() ) : the_post();
3
  • FYI: You have curly quotes in your code sample.
    – t31os
    Commented Jul 22, 2011 at 9:15
  • I tried this, but it listed posts that had nothing to do with the particular custom taxonomy.
    – Carson
    Commented Jul 22, 2011 at 19:30
  • 2
    That was not really your question.
    – Wyck
    Commented Jul 23, 2011 at 0:23

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.