# _get_term_children()

URL: https://chugunov.pro/api-wordpress/functions/_get_term_children/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 2.3.0.

## Сигнатура

```php
_get_term_children( int $term_id, array $terms, string $taxonomy, array $ancestors = array() ): array|WP_Error
```

## Описание

Если $terms — массив объектов, то _get_term_children() возвращает массив объектов.
Если $terms — массив ID, то _get_term_children() возвращает массив ID.

## Параметры

- `$term_id` `int` — обязательный. Термин-предок: все возвращаемые термины должны быть потомками $term_id.
- `$terms` `array` — обязательный. Набор терминов — массив объектов терминов или ID терминов — из которого будут выбраны те, что являются потомками $term_id.
- `$taxonomy` `string` — обязательный. Таксономия, которая определяет иерархию терминов.
- `$ancestors` `array` — необязательный, по умолчанию `array()`. Уже определённые предки термина. Передаётся по ссылке, чтобы отслеживать найденные термины при рекурсивном обходе иерархии. Массив найденных предков используется для предотвращения бесконечной рекурсии. Для производительности в качестве ключей массива используются term_id со значением 1.

## Возвращаемое значение

`array|WP_Error`

## Исходный код

Файл: `wp-includes/taxonomy.php:3972`

```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;
}
```

## История изменений

- 2.3.0 — Introduced.

## Связанные

Использует: [`_get_term_hierarchy`](https://chugunov.pro/api-wordpress/functions/_get_term_hierarchy/), [`_get_term_children`](https://chugunov.pro/api-wordpress/functions/_get_term_children/), [`get_term`](https://chugunov.pro/api-wordpress/functions/get_term/), [`is_wp_error`](https://chugunov.pro/api-wordpress/functions/is_wp_error/).
Используется в: `WP_Term_Query::get_terms`, [`_get_term_children`](https://chugunov.pro/api-wordpress/functions/_get_term_children/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/_get_term_children/
