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.

My question is simple, I'm using WP_Query to retrieve some custom type posts filtering by a taxonomy using tax_query.

Now my problem is I would like to orderby the taxonomy, but from documentation and searching in the web I can't find a solution.

The orderby in WP_Query let's you order by a bunch of fields even custom meta fields but it doesn't seem to support taxonomy.

Any pointers on the right direction?

Thank you all.

share|improve this question

4 Answers 4

up vote 10 down vote accepted

No, it is not possible to order by taxonomy, because from a certain type of standpoint, that doesn't actually make much sense.

Taxonomies are ways to group things together. So the point of having a taxonomy on posts would really be to have terms in that taxonomy that are shared between posts. If a taxonomy had terms that were only used on one post each, then that would make the taxonomy kind of pointless. And if the terms were shared like they should be, then ordering by it wouldn't produce anything particularly useful.

What you should be using in such a situation is the post meta. You can order by post meta, and it's unique to each post.

Edit: That said, you can order by taxonomy by making a custom SQL query using a filter, you just can't do it from a unmodified WP_Query: http://scribu.net/wordpress/sortable-taxonomy-columns.html

However, if you're having to resort to doing this sort of thing, then your data design structure is wrong in the first place. "Terms" in the taxonomy are not actual "data". The terms themselves have no inherent meaning, they're just labels for the particular grouping that they're describing. If you're treating them as meaningful data, then you have an underlying design flaw.

Taxonomies group things by assigning terms to them. That grouping is the whole point of taxonomies, the terms are just pretty faces on the grouping. If you have meaningful metadata to assign to a post, then you should be using the post meta for it instead. And that you can order by, because post meta uses both keys and values to store information. With a taxonomy, you're really only storing keys, with their values being the posts grouped together by that term.

Things are easier in the long run if you use the correct approach for it. While I'm not saying that you can't do something strange with taxonomy, you're just making things harder for yourself in the long run by using it wrong.

share|improve this answer
Hello Otto, thank you for the answer. I see your point and maybe I am going the wrong way with this. In my example a site of tv shows, I have taxonomy for series 1, serie 2, serie 3 etc.. So I can group all the different tv shows by series number. Then I have the same for episodes, Episode 01, Episode 02, etc.. What I would like is when showing a list of all the episodes to be order by episode and serie. I will analyse then post meta and custom fields. Thank you Otto. –  yeope Apr 8 '11 at 16:35
@yeope your taxonomy should be series and your terms should be series 1, series 2 etc. With episodes I'm assuming a series contains multiple episodes so it could use the same taxonomy, "series" and if they are hierarchal then episode 1, episode 2 etc would have the parent term "series x". Then you could query a whole series in order with the episodes falling in line where they should. –  Chris_O Apr 8 '11 at 19:17
@Chris_O I see, you might be on the money there! The only problem I can see is the fact of having to repeat the terms "Episode 1", "Episode 2" for each series. Also not being able to group all episodes 1 not depending on the series, but I think there is probably a way around it. Thank you Chris_O –  yeope Apr 8 '11 at 22:26
Using a taxonomy for episodes doesn't make much sense, actually, because the grouping is worthless. Think about it, if you have "episode 1" as a term, then you're grouping episode 1 with every other episode 1 from every other TV show. The episode and series numbers make more sense as post_meta, because they're specific to that particular show, and not useful as a group. The name of the TV show would be useful as a term in a tv-show taxonomy, because then you're grouping the show as a whole together. –  Otto Apr 15 '11 at 19:28
Otto followed this up with an interesting blog post: When to (not) use a Custom Taxonomy. –  Jan Fabry May 5 '11 at 6:55

Yes, but it is pretty involved...

Add to functions.php in your theme:

function orderby_tax_clauses( $clauses, $wp_query ) {
    global $wpdb;
    $taxonomies = get_taxonomies();
    foreach ($taxonomies as $taxonomy) {
        if ( isset( $wp_query->query['orderby'] ) && $taxonomy == $wp_query->query['orderby'] ) {
            $clauses['join'] .=<<<SQL
LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
            $clauses['where'] .= " AND (taxonomy = '{$taxonomy}' OR taxonomy IS NULL)";
            $clauses['groupby'] = "object_id";
            $clauses['orderby'] = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) ";
            $clauses['orderby'] .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC';
    return $clauses;

    add_filter('posts_clauses', 'orderby_tax_clauses', 10, 2 );

This is frankensteined from some found stuff and some stuff I did myself. Explaining is pretty tough, but the bottom line is with this running, you may put ?orderby=(taxonomy query var)&order=ASC (or DESC) and she'll take right off!

share|improve this answer
Thank you Drew, I will give a go and try to run that SQL, need to edit a bit, but it might work. My only problem now, is I might be going on the wrong directions as pointed by Otto. Thank you Drew. EDIT- No need to edit I can see where it needs the tweaking :) Thank you –  yeope Apr 8 '11 at 16:45
If you grabbed it within the last two minutes, it won't work, go ahead and grab it now, I fixed it. It was set for two specific taxonomies, I improved the code to work on all registered taxonomies. –  Drew Gourley Apr 8 '11 at 16:48
thank you once again. Just in case I tried your solution and it kind of works. Also if someone else wants to use it you need to change add_filter('posts_clauses', 'orderby_tax_clauses', 10, 2 ); to add_filter('posts_clauses', 'todo_tax_clauses', 10, 2 ); Thank you :) –  yeope Apr 8 '11 at 22:29
Yes, this is now fixed in the code block, I took this from a project I'm working on and forgot to change the name of the function even though I changed it in the hook. –  Drew Gourley Apr 9 '11 at 6:39
Do you know if it's possible to order the taxonomies by ID instead of the name? I'm trying to get the same result ordering the taxonomy groups by ID –  javiervd Jul 16 '13 at 22:43

The accepted answer for this question is unacceptable. It is illogical to assume that ordering by tax "doesn't make sense". The answer he gave doesn't make sense.

Consider having a menu post type. Then you have a custom tax of "FoodCategories". The FoodCategories tax has "Breakfast", "Lunch" and "Dinner" terms. If you submit a query utilizing the tax_query param, you now have a result set with all of the terms, however they are ordered by post date.

In order to get the correct order out of these, relative to their terms, and then to display on the front end appropriately by separating the posts into their various categories, you have to loop through the result set, then query each individual post within the result set to find it's terms and compare to the current term, filter into an array and continue throughout. Then you have to again loop through the new array for display. This is not productive.

It would be nice if WP had a "tax__in" orderby option as it does a "post__in" one, but since it does not, you either have to do the above ridiculous process; customize the query yourself by way of 'posts_orderby' filter and 'posts_join' filter in order to adjust the orderby method and add the term to the result set, respectively; or you have to make a new query for each term you are filtering for within the html sections relative to those terms.

The most efficient would be to change the query string by way of filters. The easiest would be to do three separate queries. The WP API should be handling ordering by tax, or any restrictive query parameters. If you are restricting a query based on certain conditions, there is a high probability that many will have need to order by those same conditions.

share|improve this answer
Sorry, but you are wrong. Ordering by taxonomy doesn't make any sense in your case either. What do you want to show? All the Breakfasts first, followed by all the Dinners, then all the Lunches? You should select what you want and the order in which you want it, but the taxonomy is just a grouping label. It's not meaningful "data" that you should be ordering by. If it is, then it shouldn't be a term in a taxonomy, you should make it a post-meta instead. –  Otto Oct 20 at 11:57

I'm coming late to the game here, but there is a simpler more WordPressy way of doing this.

Build out your tax query like normal.

$tax_query = array();
$tax_query[] = array(
    'taxonomy' => 'product_cat',
    'field'    => 'slug',
    'terms'    => $cat_terms,
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

Set up your args for query_posts or WP_Query

$args = array(
    'tax_query' => $tax_query,

Before you make your query_posts / WP_Query call, hook into the orderby filter and override it

add_filter('posts_orderby', 'edit_posts_orderby');
function edit_posts_orderby($orderby_statement) {
    $orderby_statement = " term_taxonomy_id ASC ";
    return $orderby_statement;
remove_filter('posts_orderby', 'edit_posts_orderby');

don't forget to remove filter afterwards...

this works b/c the tax_query creates the joins etc for you, you just need to order by one of the fields from the join.

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.