term_is_ancestor_of() WordPress Function

The term_is_ancestor_of() WordPress function is used to check if a given term is an ancestor of another term. The function returns true if the given term is an ancestor of the other term, otherwise false.

term_is_ancestor_of( int|object $term1, int|object $term2, string $taxonomy ) #

Checks if a term is an ancestor of another term.


Description

You can use either an ID or the term object for both parameters.


Top ↑

Parameters

$term1

(int|object)(Required)ID or object to check if this is the parent term.

$term2

(int|object)(Required)The child term.

$taxonomy

(string)(Required)Taxonomy name that $term1 and $term2 belong to.


Top ↑

Return

(bool) Whether $term2 is a child of $term1.


Top ↑

Source

File: wp-includes/taxonomy.php

function term_is_ancestor_of( $term1, $term2, $taxonomy ) {
	if ( ! isset( $term1->term_id ) ) {
		$term1 = get_term( $term1, $taxonomy );
	}
	if ( ! isset( $term2->parent ) ) {
		$term2 = get_term( $term2, $taxonomy );
	}

	if ( empty( $term1->term_id ) || empty( $term2->parent ) ) {
		return false;
	}
	if ( $term2->parent === $term1->term_id ) {
		return true;
	}

	return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy );
}


Top ↑

Changelog

Changelog
VersionDescription
3.4.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
Show More