get_term_children

Advertisement

Summery Summery

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

Syntax Syntax

get_term_children( int $term_id, string $taxonomy )

Description Description

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.

Parameters Parameters

$term_id

(int) (Required) ID of Term to get children.

$taxonomy

(string) (Required) Taxonomy Name.

Return Return

(array|WP_Error) List of Term IDs. WP_Error returned if $taxonomy does not exist.

Source Source

File: wp-includes/taxonomy.php

			break;
		case 'term_taxonomy_id':
			$args['term_taxonomy_id'] = $value;
			unset( $args['taxonomy'] );
			break;
		default:
			return false;
	}

	$terms = get_terms( $args );
	if ( is_wp_error( $terms ) || empty( $terms ) ) {
		return false;
	}

	$term = array_shift( $terms );

	// In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the DB.
	if ( 'term_taxonomy_id' === $field ) {
		$taxonomy = $term->taxonomy;
	}

	return get_term( $term, $taxonomy, $output, $filter );
}

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

Advertisement

Changelog Changelog

Changelog
Version Description
2.3.0 Introduced.

Advertisement

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.