Alert: 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.

_get_term_hierarchy() WordPress Function

The get_term_hierarchy() function is used to get an array of all the terms in a given taxonomy, ordered by their hierarchy. This is useful for displaying a list of terms in a hierarchical order.

_get_term_hierarchy( string $taxonomy ) #

Retrieves children of taxonomy as term IDs.


Parameters

$taxonomy

(string)(Required)Taxonomy name.


Top ↑

Return

(array) Empty if $taxonomy isn't hierarchical or returns children as term IDs.


Top ↑

Source

File: wp-includes/taxonomy.php

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


Top ↑

Changelog

Changelog
VersionDescription
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More