WordPress at a glance

term_is_ancestor_of() WP 1.0

Check if a term is an ancestor of another term.

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

Is the basis for: cat_is_ancestor_of()
✈ 1 time = 0.000142s = fast | 50000 times = 0.87s = very fast PHP 7.0.14, WP 4.7

No Hooks.

Return

true/false. Whether $term2 is a child of $term1.

Usage

term_is_ancestor_of( $term1, $term2, $taxonomy );
$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.

Code of term_is_ancestor_of: wp-includes/taxonomy.php VER 5.0.1

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

Related Functions

From tag: Conditional tags (all)

More from category: Any taxonomy

No comments
    Hello, !     Log In . Register