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?

  • 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. 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? Sep 20 '11 at 21:04

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.

  • @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". 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. May 26 '11 at 15:45
  • 1
    "Order & Orderby" Parameters: "Multiple" Example codex.wordpress.org/Function_Reference/WP_Query#Parameters
    – kaiser
    May 26 '11 at 15:48
  • Ahh, thank you! Does it prioritize based on the order you put it in? Also, it notes a comma - Can you use a comma or space interchangeably? May 26 '11 at 22:35

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.