I'm using code below to spit out custom post types along with its categories, so

Category 1 ---post 1 ---post 2

Category 2 ---post 1 ---post 2 ---post 3 etc.

That works well for me, however I would like to be able to sort my categories by count so orderby=count so catgories with larger amount of post will go to the top of the list at the moment that is not working in my code below, any idea why?

Many thanks for help


// List posts by the terms for a custom taxonomy of any post type

$post_type = 'our_drinks';
$tax = ('our-drinks');
$tax_terms = get_terms( $tax, 'orderby=count&order=ASC&hide_empty=0&hierarchical=true');
if ($tax_terms) {
    foreach ($tax_terms  as $tax_term) {
    $args = array(
        'post_type' => $post_type,
        "$tax" => $tax_term->slug,
        'post_status' => 'publish',
        'order' => 'ASC',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1

        $my_query = null;
        $my_query = new WP_Query($args);

        if( $my_query->have_posts() ) : ?>

                     <div class="category_title">

                                <?php echo $tax_term->name; ?>

             <?php while ( $my_query->have_posts() ) : $my_query->the_post();  ?>

                                        LOOP CONTENT GOES HERE

                        <?php endwhile; ?>  

                  v<?php else : ?>
        <?php endif; // if have_posts()

    } // end foreach #tax_terms

2 Answers 2


You have orderby=count, which should be correct, but you also have order=asc. order=ASC means "smallest to largest", at least with numbers. You want the opposite, "largest to smallest". Use order=DESC instead. I suspect that get_terms is working, you've just told it something different that you actually want.

Also, you have multiple queries in a loop and no real limits on it. That could result in a huge number of database queries and a noticeable impact on server performance. Be aware.

  • thank you very much yes DESC worked for me,very easy fix, thanks again :)
    – jmysona
    Mar 26, 2013 at 15:09

Since you didn't tell how "this doesn't work", I will cover every problem that I can think right now.

  1. Put into your register_taxonomy function argument query_var => 'our-drinks', if you already have not one.
  2. If doesn't work, or you have it already, change the args in $my_query query to:

    $args = array(
            'post_type' => $post_type,
            'tax_query' => array(
                    'taxonomy' => $tax,
                    'field' => 'slug',
                    'terms' => $tax_term->slug,
            'post_status' => 'publish',
            'order' => 'ASC',
            'posts_per_page' => -1,
            'caller_get_posts'=> 1

    since your way of doing it is somewhat deprecated. Details about tax_query you can find here. Notice nested arrays - it's not a bug :)

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.