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

_get_term_children() WordPress Function

The get_term_children() function can be used to get an array of all the child terms of a given parent term. This is useful for creating hierarchical taxonomies.

_get_term_children( int $term_id, array $terms, string $taxonomy, array $ancestors = array() ) #

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


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.


Top ↑

Parameters

$term_id

(int)(Required)The ancestor term: all returned terms should be descendants of $term_id.

$terms

(array)(Required)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

(string)(Required)The taxonomy which determines the hierarchy of the terms.

$ancestors

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


Top ↑

Return

(array|WP_Error) The subset of $terms that are descendants of $term_id.


Top ↑

Source

File: wp-includes/taxonomy.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;
}


Top ↑

Changelog

Changelog
VersionDescription
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More