WordPress at a glance

get_term_children() WP 1.0

Merge all term children into a single array of their IDs.

This recursive function will merge all of the children of $term into the same array of term IDs. Only useful for taxonomies which are hierarchical.

Will return an empty array if $term does not exist in $taxonomy.

Works based on: _get_term_hierarchy()
✈ 1 time = 0.000048s = very fast | 50000 times = 0.75s = very fast PHP 7.0.5, WP 4.5.2

No Hooks.

Return

Array/WP_Error. List of Term IDs. WP_Error returned if $taxonomy does not exist.

Usage

get_term_children( $term_id, $taxonomy );
$term_id(int) (required)
ID of Term to get children.
$taxonomy(string) (required)
Taxonomy Name.

Code of get_term_children: wp-includes/taxonomy.php VER 4.9.8

<?php
function get_term_children( $term_id, $taxonomy ) {
	if ( ! taxonomy_exists( $taxonomy ) ) {
		return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
	}

	$term_id = intval( $term_id );

	$terms = _get_term_hierarchy($taxonomy);

	if ( ! isset($terms[$term_id]) )
		return array();

	$children = $terms[$term_id];

	foreach ( (array) $terms[$term_id] as $child ) {
		if ( $term_id == $child ) {
			continue;
		}

		if ( isset($terms[$child]) )
			$children = array_merge($children, get_term_children($child, $taxonomy));
	}

	return $children;
}

Related Functions

From tag: Relations (of posts terms...)

More from tag: Term (taxonomies terms)

More from category: Any taxonomy

No comments
    Hello, !     Log In . Register