_get_term_hierarchy()WP 2.3.0

Retrieves children of taxonomy as term IDs.

Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.

No Hooks.

Return

Array. Empty if $taxonomy isn't hierarchical or returns children as term IDs.

Usage

_get_term_hierarchy( $taxonomy );
$taxonomy(string) (required)
Taxonomy name.

Changelog

Since 2.3.0 Introduced.

_get_term_hierarchy() code WP 6.4.3

function _get_term_hierarchy( $taxonomy ) {
	if ( ! is_taxonomy_hierarchical( $taxonomy ) ) {
		return array();
	}
	$children = get_option( "{$taxonomy}_children" );

	if ( is_array( $children ) ) {
		return $children;
	}
	$children = array();
	$terms    = get_terms(
		array(
			'taxonomy'               => $taxonomy,
			'get'                    => 'all',
			'orderby'                => 'id',
			'fields'                 => 'id=>parent',
			'update_term_meta_cache' => false,
		)
	);
	foreach ( $terms as $term_id => $parent ) {
		if ( $parent > 0 ) {
			$children[ $parent ][] = $term_id;
		}
	}
	update_option( "{$taxonomy}_children", $children );

	return $children;
}