_get_term_children

Advertisement

Private Access Private Access

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.

Summery Summery

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

Syntax Syntax

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

Description 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.

Parameters Parameters

$term_id

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

$terms

(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

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

$ancestors

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

Return Return

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

Source Source

File: wp-includes/taxonomy.php

	$non_cached_ids = array_unique( $non_cached_ids );

	$terms = wp_get_object_terms(
		$non_cached_ids,
		$taxonomies,
		array(
			'fields'                 => 'all_with_object_id',
			'orderby'                => 'name',
			'update_term_meta_cache' => false,
		)
	);

	$object_terms = array();
	foreach ( (array) $terms as $term ) {
		$object_terms[ $term->object_id ][ $term->taxonomy ][] = $term->term_id;
	}

	foreach ( $non_cached_ids as $id ) {
		foreach ( $taxonomies as $taxonomy ) {
			if ( ! isset( $object_terms[ $id ][ $taxonomy ] ) ) {
				if ( ! isset( $object_terms[ $id ] ) ) {
					$object_terms[ $id ] = array();
				}
				$object_terms[ $id ][ $taxonomy ] = array();
			}
		}
	}

	foreach ( $object_terms as $id => $value ) {
		foreach ( $value as $taxonomy => $terms ) {
			wp_cache_add( $id, $terms, "{$taxonomy}_relationships" );
		}
	}
}

/**
 * Updates Terms to Taxonomy in cache.
 *
 * @since 2.3.0
 *
 * @param WP_Term[] $terms    Array of term objects to change.
 * @param string    $taxonomy Not used.
 */
function update_term_cache( $terms, $taxonomy = '' ) {
	foreach ( (array) $terms as $term ) {
		// Create a copy in case the array was passed by reference.
		$_term = clone $term;

		// Object ID should not be cached.
		unset( $_term->object_id );

		wp_cache_add( $term->term_id, $_term, 'terms' );
	}
}

Advertisement

Changelog Changelog

Changelog
Version Description
2.3.0 Introduced.

Advertisement

Leave a Reply