Using wp_query is it possible to orderby taxonomy?

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 on the web I can’t find a solution.

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

Any pointers in the right direction?

Thank you all.

1
11

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.

Leave a Comment