_get_term_children( int $term_id, array $terms, string $taxonomy, array $ancestors = array() ): array|WP_Error
- Since
- 2.3.0
- Source
wp-includes/taxonomy.php:3968
Description
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.
Compatibility
- WordPress
- since 2.3.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$term_idint- The ancestor term: all returned terms should be descendants of
$term_id. $termsarray- 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.
$taxonomystring- The taxonomy which determines the hierarchy of the terms.
$ancestorsarrayoptional- 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_idsare used as array keys, with 1 as value. Default empty array.Default:array()
Return value
array|WP_Error- The subset of $terms that are descendants of $term_id.
Performance profile
How much work a call to _get_term_children() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 7–37
- Plugin surface
- None
- Called by
- 2
Reaches the database via get_term().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 78.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_term()called directly - optionoption read or write
get_option()one call below _get_term_children() - hookthird-party callbacks
apply_filters()one call below _get_term_children()
Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _get_term_children() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($terms) | 7 | none |
!empty($terms) | 17–25 | _get_term_hierarchy() |
!empty($terms) && !$terms && !is_object($term) && is_wp_error() | 33–37 | _get_term_hierarchy(), get_term(), is_wp_error() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 78 instructions, 7–37 executed per call, 13 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 4
- _get_term_hierarchy()Retrieves children of taxonomy as term IDs.
- get_term()Gets all term data from database by term ID.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- _get_term_children()Gets the subset of $terms that are descendants of $term_id.
Used by · 2
- WP_Term_Query::get_terms()Retrieves the query results.
- _get_term_children()Gets the subset of $terms that are descendants of $term_id.
Source code
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;}Changelog
Introduced in 2.3.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/taxonomy.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.