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
add comment

2 Answers

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
2  
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
1  
Otto followed this up with an interesting blog post: When to (not) use a Custom Taxonomy. –  Jan Fabry May 5 '11 at 6:55
show 5 more comments

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!

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
1  
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
add comment

Your Answer

 
discard

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.