Before anyone say that I didn't try let me say I tried a lot to achieve this. I even achieved but when I add the following code to functions.php the sorting works but the filters on left side doesn't work.

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 particular code

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

is creating the problem I think because when I remove this line it filters properly. I think its creating some query conflict and giving " No results found" when I use both filter and soering option. When I use only orting it works and when I use only filter it works. How to make both work together ?

This is the site:


Please help me out!

Thank you so much!


  • The title, the content and the code you have posted make some unclear cocktel, at least for me. Can you explain exactly what are you trying to do and what is the result you want to get?
    – cybmeta
    Sep 20, 2013 at 18:04
  • on my site thereare filters on left column and sorting bar on the top. Before I put the sorting bar the filters were working fine . I could filter any institution or platform. The results used to come up in a random order. So I put a sorting bar.Sorting bar should sort each taxonomy in ASC order and DSEC order. If you type " Engneering" and search it will show all the results and u can sort them from the bar. To make this sorting work I put that code on functions.php. So After search you do sorting then u use the platform filter . The results say "No results found. ".
    – Ray
    Sep 20, 2013 at 18:21
  • But if you dont use the sorting option and filter it only then the results sho up . The right time u use sorting and filter together it doesn't work.
    – Ray
    Sep 20, 2013 at 18:22

1 Answer 1


If you really need to sort by taxonomy term, I think you have a bad design of the data structure in your application. Think about it.

A taxonomy is a way to group things, lets say posts. A taxonomy is a group of "terms" and the posts are grouped by the shared "terms" between them. From my point of view, if the terms are used as they should be, sorting by terms is not useful at all and have no sense.

Lets go with an example in your page: You can filter by "Codecademy" platform, so you get the group of items assigned to "Codecademy". Then you click in the "platform" table header to sort by "Codecademy", where the logic is?. It is like having a group of "dentists" that you want to sort by "dentists"; you may want to sort by something that describe each dentist, like name or cost, but not by "dentists". Do you get my point? If you need to sort by some posts characteristic, you should use customs post meta fields or core post fields, not taxonomies.

Another example, if you want to sort alphabetically by the "platform" taxonomy, what you really want is sort the terms of a taxonomy alphabetically and the show the posts for each term, this is not sorting the posts, it is sorting the terms.

Anyway, if you really want to sort by taxonomy term you can do it, not by a standard way provided by Wordpress but by a custom way like your workaround in the custom SQL you are using or the example of this article.

  • I understand totally what you mean but in this case I am not the one who can decide everything. :( So can you tell me how to do the sorting in ASC and DSEC order? May be the columns will change later with something else so I need to know. EDIT: "should not try to filter and to sort by the same term at the same time." - Cant I do that? WHat if I want to sort hrs/week or Cost?
    – Ray
    Sep 20, 2013 at 18:54
  • I can't not say to you how to sort and group by the same thing at the same time as I can not say how you can sort people by "brown hair" in ASC or DESC order. I can say how to separate people with "brown hair" from people with a different colour hair. Maybe some other code guru can.
    – cybmeta
    Sep 20, 2013 at 19:09
  • BTW, cost or hrs/week should be meta data, not terms of a taxonomies, as well as any other field you need to sort by.
    – cybmeta
    Sep 20, 2013 at 19:10
  • I wanted to sort alphabetically the platform and institution . So all the Canvas courses will show up then all Coursera courses will show up and so on..The title works currently ( ASC/ DSEC) order. The hrs/week, StartDate & Cost I wanted to sort by ASC DSEC order.
    – Ray
    Sep 20, 2013 at 19:17
  • I understand what you want to do, I just think that you have an incorrect approach. Use custom post meta fields for all the fields you need to sort by, it will be more esay, fast, accurate and appropiate. You need to understand this: what you want by sorting the posts by a taxonomy, for example, sorting alphabetically by the platform, it is really sorting the terms of the taxonomy alphabetically and then show the posts for each term, it is not sorting the posts.
    – cybmeta
    Sep 20, 2013 at 19:32

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.