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( string $taxonomy )

Retrieves children of taxonomy as Term IDs.


Description Description


Parameters Parameters

$taxonomy

(string) (Required) Taxonomy name.


Top ↑

Return Return

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


Top ↑

Source 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

Changelog
Version Description
2.3.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex
       /** The taxonomy we want to parse */
       $taxonomy = "category";
       /** Get all taxonomy terms */
       $terms = get_terms($taxonomy, array(
               "orderby"    => "count",
               "hide_empty" => false
           )
       );
       /** Get terms that have children */
       $hierarchy = _get_term_hierarchy($taxonomy);
           /** Loop through every term */
           foreach($terms as $term) {
           //Skip term if it has children
           if($term->parent) {
             continue;
           }
             echo $term->name;
           /** If the term has children... */
             if($hierarchy[$term->term_id]) {
           /** display them */
           foreach($hierarchy[$term->term_id] as $child) {
           /** Get the term object by its ID */
           $child = get_term($child, "category_list");
                echo '--'.$child->name;
               }
            }
         }
    

You must log in before being able to contribute a note or feedback.