Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to order my archive page by comment_count then title.

add_action( 'pre_get_posts', 'my_change_sort_order');
function my_change_sort_order($query)
{
    if (is_archive()) {
       $query->set( 'orderby', 'comment_count title' );
    }
}

However, comment_count is ignored and I just get my posts in descending order of title.

It works fine if I just order by comment_count alone:

add_action( 'pre_get_posts', 'my_change_sort_order');
function my_change_sort_order($query)
{
    if (is_archive()) {
       $query->set( 'orderby', 'comment_count' );
    }
}

Any ideas what I'm doing wrong? Or have I found a bug?

share|improve this question
    
Sadly, that won't work. You'll need to use the post_orderby filter. I'm not sure if this would be considered a duplicate question but the answer to this is the same as these other two. Answer 1: wordpress.stackexchange.com/questions/91890/… Answer 2: wordpress.stackexchange.com/questions/70214/… –  epilektric Aug 22 '13 at 5:21
    
That did it, many thanks! In case anyone else is trying to order by comment_count then title, the custom orderby string is `$wpdb->posts.'.comment_count DESC, ' . $wpdb->posts.'.post_title ASC´. –  adamf321 Aug 31 '13 at 17:22
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.