get_term_children

Merge all term children into a single array of their IDs.

Description

(array|WP_Error) _get_term_children( (int) $term_id, (array) $terms, (string) $taxonomy, (array) &$ancestors = array() ); 

If $terms is an array of objects, then _get_term_children() returns an array of objects. If $terms is an array of IDs, then _get_term_children() returns an array of IDs.

Returns (array|WP_Error)

The subset of $terms that are descendants of $term_id.

Parameters (4)

0. $term_id (int)
The ancestor term: all returned terms should be descendants of $term_id..
1. $terms (array)
The set of terms - either an array of term objects or term IDs - from which those that are descendants of $term_id will be chosen.
2. $taxonomy (string)
The taxonomy which determines the hierarchy of the terms.
3. $ancestors — Optional. (array) => &$ancestors = array()
Term ancestors that have already been identified. Passed by reference, to keep track of found terms when recursing the hierarchy. The array of located ancestors is used to prevent infinite recursion loops. For performance, term_ids are used as array keys, with 1 as value. Default empty array.

Usage

  1. if ( !function_exists( '_get_term_children' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // The ancestor term: all returned terms should be descendants of `$term_id`. 
  5. $term_id = -1; 
  6.  
  7. // The set of terms - either an array of term objects or term IDs - from which those that 
  8. // are descendants of $term_id will be chosen. 
  9. $terms = array(); 
  10.  
  11. // The taxonomy which determines the hierarchy of the terms. 
  12. $taxonomy = ''; 
  13. $ancestors = array(); 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = _get_term_children($term_id, $terms, $taxonomy, $ancestors); 
  17.  

Defined (2)

The function is defined in the following location(s).

/wp-includes/taxonomy.php  
  1. function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) { 
  2. $empty_array = array(); 
  3. if ( empty($terms) ) 
  4. return $empty_array; 
  5.  
  6. $term_list = array(); 
  7. $has_children = _get_term_hierarchy($taxonomy); 
  8.  
  9. if ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) ) 
  10. return $empty_array; 
  11.  
  12. // Include the term itself in the ancestors array, so we can properly detect when a loop has occurred. 
  13. if ( empty( $ancestors ) ) { 
  14. $ancestors[ $term_id ] = 1; 
  15.  
  16. foreach ( (array) $terms as $term ) { 
  17. $use_id = false; 
  18. if ( !is_object($term) ) { 
  19. $term = get_term($term, $taxonomy); 
  20. if ( is_wp_error( $term ) ) 
  21. return $term; 
  22. $use_id = true; 
  23.  
  24. // Don't recurse if we've already identified the term as a child - this indicates a loop. 
  25. if ( isset( $ancestors[ $term->term_id ] ) ) { 
  26. continue; 
  27.  
  28. if ( $term->parent == $term_id ) { 
  29. if ( $use_id ) 
  30. $term_list[] = $term->term_id; 
  31. else 
  32. $term_list[] = $term; 
  33.  
  34. if ( !isset($has_children[$term->term_id]) ) 
  35. continue; 
  36.  
  37. $ancestors[ $term->term_id ] = 1; 
  38.  
  39. if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors) ) 
  40. $term_list = array_merge($term_list, $children); 
  41.  
  42. return $term_list; 
  1. function get_term_children( $term_id, $taxonomy ) { 
  2. if ( ! taxonomy_exists( $taxonomy ) ) { 
  3. return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); 
  4.  
  5. $term_id = intval( $term_id ); 
  6.  
  7. $terms = _get_term_hierarchy($taxonomy); 
  8.  
  9. if ( ! isset($terms[$term_id]) ) 
  10. return array(); 
  11.  
  12. $children = $terms[$term_id]; 
  13.  
  14. foreach ( (array) $terms[$term_id] as $child ) { 
  15. if ( $term_id == $child ) { 
  16. continue; 
  17.  
  18. if ( isset($terms[$child]) ) 
  19. $children = array_merge($children, get_term_children($child, $taxonomy)); 
  20.  
  21. return $children;