获取$ term_id后代的$ terms子集。

原型

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

描述

如果$ terms是一个对象数组,则_get_term_children()返回一个对象数组。如果$ terms是一个ID数组,则_get_term_children()返回一个ID数组。

参数

$term_id (int) (Required) 祖先术语:所有返回的术语应该是$ term_id的后代。

$terms (array) (Required) 一组术语 – 术语对象或术语ID的数组 – 将从中选择$ term_id的后代。

$taxonomy (string) (Required) 用于确定术语层次结构的分类法。

$ancestors (array) (Optional)  已经确定的术语祖先。通过引用传递,以在递归层次结构时跟踪找到的术语。定位祖先数组用于防止无限递归循环。对于性能,term_ids用作数组键,值为1。

返回值

(array|WP_Error)  作为$ term_id后代的$ terms子集。

源文件

路径:wp-includes/taxonomy.php

<?php
...
function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) {
	$empty_array = array();
	if ( empty($terms) )
		return $empty_array;
	$term_list = array();
	$has_children = _get_term_hierarchy($taxonomy);
	if  ( ( 0 != $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 ( $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;
			if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors) )
				$term_list = array_merge($term_list, $children);
		}
	}
	return $term_list;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/_get_term_children/