`get_terms()` with `child_of` and `childless` combined

I have a hierarchical taxonomy filter that contains locations and genres for posts with the custom type artist:

- Genre
-- Hip Hop
-- Trap
-- Rap
- Location
-- Europe
--- Germany
--- Sweden
--- Austria
-- Asia
--- China
--- Japan
--- Taiwan

Now I would like to use get_terms() to only get the Countries (children of 'Location' without children of their own). I thought this should work:

$location_parent = get_term(123, 'filter');

$countries = get_terms(array(
  'taxonomy' = $location_parent-taxonomy,
  'hide_empty' = false,
  'child_of' = $location_parent-term_id, 
  'childless' = true
));

...but it somehow doesn't. child_of and childless seem to get in each other's way. Any ideas?

Topic children terms taxonomy Wordpress

Category Web


OK, so this is what I came up with:

function get_childless_term_children( $parent_id, $taxonomy ) {
  // get all childless $terms of this $taxonomy
  $terms = get_terms(array(
    'taxonomy' => $taxonomy,
    'childless' => true,
  ));

  foreach( $terms as $key => $term ) {
    // remove $terms that aren't descendants (= children) of $parent_id
    if( !in_array( $parent_id, get_ancestors( $term->term_id, $taxonomy ) ) ) {
      unset( $terms[$key] );
    }
  }
  return $terms;
}

What this does: Get all childless terms that are children of $parent_id.


childless means:

(boolean) Returns terms that have no children if taxonomy is hierarchical, all terms if taxonomy is not hierarchical

Reference: https://codex.wordpress.org/es:Function_Reference/get_terms

I think you're finding something like this:

/**
 * Recursively get taxonomy hierarchy
 *
 * @source http://www.daggerhart.com/wordpress-get-taxonomy-hierarchy-including-children/
 * @param string $taxonomy
 * @param int    $parent - parent term id
 *
 * @return array
 */
function get_taxonomy_hierarchy( $taxonomy, $parent = 0 ) {
    // only 1 taxonomy
    $taxonomy = is_array( $taxonomy ) ? array_shift( $taxonomy ) : $taxonomy;
    // get all direct decendents of the $parent
    $terms = get_terms( $taxonomy, array('parent' => $parent) );
    // prepare a new array.  these are the children of $parent
    // we'll ultimately copy all the $terms into this new array, but only after they
    // find their own children
    $children = array();
    // go through all the direct decendents of $parent, and gather their children
    foreach( $terms as $term ) {
        // recurse to get the direct decendents of "this" term
        $term->children = get_taxonomy_hierarchy( $taxonomy, $term->term_id );
        // add the term to our new array
        $children[ $term->term_id ] = $term;
    }
    // send the results back to the caller
    return $children;
}

It should return all the child from Location

We are agree?

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.