get_term_children

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

Description

(array|WP_Error) get_term_children( (string) $term_id, (string) $taxonomy ); 

This recursive function will merge all of the children of $term into the same array of term IDs. Only useful for taxonomies which are hierarchical.

Will return an empty array if $term does not exist in $taxonomy.

Returns (array|WP_Error)

List of Term IDs. WP_Error returned if `$taxonomy` does not exist.

Parameters (2)

0. $term_id (string)
ID of Term to get children.
1. $taxonomy (string)
Taxonomy Name.

Usage

  1. if ( !function_exists( 'get_term_children' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // ID of Term to get children. 
  5. $term_id = ''; 
  6.  
  7. // Taxonomy Name. 
  8. $taxonomy = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_term_children($term_id, $taxonomy); 
  12.  

Defined (2)

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

/wp-includes/taxonomy.php  
  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; 
  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;