Wordpress - Using wp_query is it possible to orderby taxonomy?

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.


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)
SQL;
            $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!


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.