WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm returning a custom hierarchal taxonomy via wp_get_object_terms.

The trouble is that the results are being ordered by name or count, neither of which are proving reliable. (I'm using a custom taxonomy for "Location" - City, State, Country - sometimes the State appears before the City)

Is there a way I can order the resulting array by the custom taxonomy hierarchy?

Is there, perhaps, another way I should be looking at this?

share|improve this question
Which solution did you go for? – Mark Sep 20 '11 at 16:20
What Kaiser detailed works well. Another option is to use term_order in the order_by. – Jonathan Wold Sep 20 '11 at 20:34
I tried term_order but that seems to be ignored :( Look at : pastebin.com/RXMipMKE – Mark Sep 20 '11 at 20:46
Also the problem with Kaisers approach is that you need to know the categories which isn't really that flexible. – Mark Sep 20 '11 at 20:49
Hmm.. Try reversing the "order" to "DESC". What does that give you? – Jonathan Wold Sep 20 '11 at 21:04
up vote 0 down vote accepted

Just a rough sketch to get your idea:

  • countries (hierarchical taxonomy)
    • France (country - sub taxonomy)
      • Champagne (state - sub sub taxonomy)
        • Le Mans (city - term)
        • Angers (city - term)
        • Nantes (city - term)

And you want to order:

$query_results = query( 'country' => 'france', 'orderby' => 'state city', 'order' => 'ASC' );

Did i get this right? If not, feel free to edit my answer or update your Q.

share|improve this answer
@kaiser - I went another route to resolve this. Out of curiosity, though, have you tested this? It appears that you have two terms in the "order by". – Jonathan Wold May 26 '11 at 15:37
I updated the Codex some month ago to show that it is possible. Just leave a space between two values. – kaiser May 26 '11 at 15:41
Really! What is the codex reference? I looked on WP_Query and didn't see that. I want to learn more about this. – Jonathan Wold May 26 '11 at 15:45
"Order & Orderby" Parameters: "Multiple" Example codex.wordpress.org/Function_Reference/WP_Query#Parameters – kaiser May 26 '11 at 15:48
Good point - Thank you :). – Jonathan Wold May 27 '11 at 2:09

You could use wp_list_categories to sort out the hierarchy, it started supporting taxonomies as of 3.0.

You can see an example of using a custom taxonomy with wp_list_categories on the codex page for the function.

You'd just need to pass in the term ids from your wp_get_object_terms call to the wp_list_categories include arg, eg.

wp_list_categories( array( 'include' => $your_array_of_ids ) );

It should then list out the terms of your custom taxonomy in a hierarchal format, though it may not appear so if your theme isn't padding list elements(so be sure to apply appropriate styling if the results aren't showing in a hierarchal form).

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.