Skip to content

Instantly share code, notes, and snippets.

@BeardedGinger
Created April 28, 2015 14:17
Show Gist options
  • Select an option

  • Save BeardedGinger/9bddfdd0fbb3368cc45d to your computer and use it in GitHub Desktop.

Select an option

Save BeardedGinger/9bddfdd0fbb3368cc45d to your computer and use it in GitHub Desktop.
get the lowest level term assigned to a post from a hierarchical taxonomy
<?php
//Find the lowest term on the totem pole
$parents = array();
$regions = get_the_terms( get_the_ID(), 'regions' );
foreach( $regions as $region ) {
$parents[] = $region->parent;
}
foreach( $regions as $region ) {
// if the current term isn't another's parent, it has to be the lowest right?
if( !in_array( $region->term_id, $parents ) ) {
$location = $region->name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment