Title: _get_term_children
Published: April 25, 2014
Last modified: February 24, 2026

---

# _get_term_children( int $term_id, array $terms, string $taxonomy, array $ancestors = array() ): array|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#wp--skip-link--target)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Gets the subset of $terms that are descendants of $term_id.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#description)󠁿

If `$terms` is an array of objects, then [_get_term_children()](https://developer.wordpress.org/reference/functions/_get_term_children/)
returns an array of objects.
If `$terms` is an array of IDs, then [_get_term_children()](https://developer.wordpress.org/reference/functions/_get_term_children/)
returns an array of IDs.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#parameters)󠁿

 `$term_id`intrequired

The ancestor term: all returned terms should be descendants of `$term_id`.

`$terms`arrayrequired

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.

`$taxonomy`stringrequired

The taxonomy which determines the hierarchy of the terms.

`$ancestors`arrayoptional

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:`array()`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#return)󠁿

 array|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) The
subset of $terms that are descendants of $term_id.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#source)󠁿

    ```php
    function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) {
    	$empty_array = array();
    	if ( empty( $terms ) ) {
    		return $empty_array;
    	}

    	$term_id      = (int) $term_id;
    	$term_list    = array();
    	$has_children = _get_term_hierarchy( $taxonomy );

    	if ( $term_id && ! isset( $has_children[ $term_id ] ) ) {
    		return $empty_array;
    	}

    	// Include the term itself in the ancestors array, so we can properly detect when a loop has occurred.
    	if ( empty( $ancestors ) ) {
    		$ancestors[ $term_id ] = 1;
    	}

    	foreach ( (array) $terms as $term ) {
    		$use_id = false;
    		if ( ! is_object( $term ) ) {
    			$term = get_term( $term, $taxonomy );
    			if ( is_wp_error( $term ) ) {
    				return $term;
    			}
    			$use_id = true;
    		}

    		// Don't recurse if we've already identified the term as a child - this indicates a loop.
    		if ( isset( $ancestors[ $term->term_id ] ) ) {
    			continue;
    		}

    		if ( (int) $term->parent === $term_id ) {
    			if ( $use_id ) {
    				$term_list[] = $term->term_id;
    			} else {
    				$term_list[] = $term;
    			}

    			if ( ! isset( $has_children[ $term->term_id ] ) ) {
    				continue;
    			}

    			$ancestors[ $term->term_id ] = 1;

    			$children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors );
    			if ( $children ) {
    				$term_list = array_merge( $term_list, $children );
    			}
    		}
    	}

    	return $term_list;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/taxonomy.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/taxonomy.php#L3968)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/taxonomy.php#L3968-L4023)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#related)󠁿

| Uses | Description | 
| [_get_term_hierarchy()](https://developer.wordpress.org/reference/functions/_get_term_hierarchy/)`wp-includes/taxonomy.php` |

Retrieves children of taxonomy as term IDs.

  | 
| [_get_term_children()](https://developer.wordpress.org/reference/functions/_get_term_children/)`wp-includes/taxonomy.php` |

Gets the subset of $terms that are descendants of $term_id.

  | 
| [get_term()](https://developer.wordpress.org/reference/functions/get_term/)`wp-includes/taxonomy.php` |

Gets all term data from database by term ID.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  |

[Show 2 more](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#)

| Used by | Description | 
| [WP_Term_Query::get_terms()](https://developer.wordpress.org/reference/classes/wp_term_query/get_terms/)`wp-includes/class-wp-term-query.php` |

Retrieves the query results.

  | 
| [_get_term_children()](https://developer.wordpress.org/reference/functions/_get_term_children/)`wp-includes/taxonomy.php` |

Gets the subset of $terms that are descendants of $term_id.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/_get_term_children/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.3.0](https://developer.wordpress.org/reference/since/2.3.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_get_term_children%2F)
before being able to contribute a note or feedback.