1

I'm using get_the_terms to display 2 terms on a post, associated with the taxonomy "location". The terms are the suburb and the city. The location taxonomy is hierarchical so the suburb is a child of the city.

In the post edit screen a user selects their city, followed by their suburb (which lists the suburbs based on the city they pick). Both are selectable from drop down menus.

My problem is that when the terms are outputted onto the post, it lists them alphabetically, and in some instances the City will be listed first:

Alpha (City), Beta (Suburb) 

I need them to reflect their Hierarchy so they are always listed by Suburb first, then city.

Beta (Suburb), Alpha, (City)

Here is my code.

<?php
$terms = get_the_terms ($post->id, 'location');

if ( !is_wp_error($terms)) : ?>

<li id="location">

<?php unset($locations);
foreach ($terms as $term) {
    $locations[] = $term->name;
    }

$location = join(", ", $locations);
?>

<?php echo $location; ?>

</li>
<?php endif ?>

Would this be better achieved with wp_list_categories somehow? It seems there is an option "hierarchical" but unsure on how to use it so it just grabs the post's terms and not everything

2 Answers 2

2

I managed to hack this together using wp_get_object_terms. The 'orderby' => 'term_id' was most helpful. It's probably not the best method, but seems to work fine. Because the child terms (suburbs) are always created after the parent terms (Cities), they will always have a higher ID.

<?php 
$terms = wp_get_object_terms($post->ID, 'location', array('orderby' => 'term_id', 'order' => 'DESC') );

if ( !empty( $terms ) ) :

    $location = array();
    foreach ( $terms as $term ) {
         $location[] = $term->name;
    }
    echo implode(', ', $location);  

endif; 
?>
1

I think the easiest way to accomplish this in your case would be to reverse the array.

foreach ($terms as $term) {
    $locations[] = $term->name;
}
$locations = array_reverse($locations);

There are more complicated ways to do this, but I think this should work for your situation.

2
  • Thanks for your answer. I'm sorry but I just realised that they are ordered by name and not simply reversed. I was testing with a few cities that started with "A", hence the City always appeared first, followed by the suburb. I just tested with a city starting with "W" and it's how it should be, suburb first, then city. Looks like I will be after a hierarchical solution after all.
    – Andrew
    Jul 12, 2011 at 5:00
  • Not the best solution, but you can use this plugin (wordpress.org/extend/plugins/custom-taxonomy-sort) to order your terms. If done correctly your problem will be solved. Otherwise, in your foreach statement do a var_dump on term and you will see that there is a 'parent' property that can be helpful in working out a solution.
    – tollmanz
    Jul 12, 2011 at 5:06

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.