Using term_is_ancestor_of to check if a term is a parent in WordPress

The term_is_ancestor_of function is a WordPress function that checks if a term is an ancestor of another term in the context of hierarchical taxonomies. This function is part of the WordPress taxonomy API that provides a set of functions and tools to manage, retrieve and manipulate terms in taxonomies.

The term_is_ancestor_of function can be used to determine the hierarchical relationship between two terms. It returns a boolean value, true if the first term is an ancestor of the second term, and false otherwise. This function can be used in various contexts where understanding the relationship between terms is necessary, such as in creating navigation menus, breadcrumbs, or displaying related content.

The function works with any hierarchical taxonomy, including but not limited to the built-in categories and post tags, as well as any custom taxonomies that may be registered.

It’s important to note that the term_is_ancestor_of function only checks direct and indirect parent-child relationships. It does not consider sibling relationships or other types of connections between terms.

Parameters Accepted by term_is_ancestor_of Function

The term_is_ancestor_of function in WordPress accepts three parameters. These parameters are as follows:

  • $term1 (intobject) – This is a mandatory parameter. It refers to the ID or the object that is being checked to ascertain if it is the parent term.
  • $term2 (intobject) – This is also a required parameter. It denotes the term that is being checked to determine if it is the child term.
  • $taxonomy (string) – This compulsory parameter represents the name of the taxonomy that $term1 and $term2 belong to.

Return Value of term_is_ancestor_of Function

The term_is_ancestor_of function returns a boolean value. This value indicates whether $term2 is a child of $term1 or not.

If the function does not accept any parameters, it is explicitly stated in the function’s documentation.

Examples

How to Check if a Term is a Child of Another Term in a Specific Taxonomy

$parent_term_id = 5;
$child_term_id = 10;
$taxonomy = 'category';

if(term_is_ancestor_of($parent_term_id, $child_term_id, $taxonomy)) {
 echo '<p>The term with ID ' . $child_term_id . ' is a child of the term with ID ' . $parent_term_id . ' in the ' . $taxonomy . ' taxonomy.</p>';
} else {
 echo '<p>The term with ID ' . $child_term_id . ' is not a child of the term with ID ' . $parent_term_id . ' in the ' . $taxonomy . ' taxonomy.</p>';
}

This code snippet checks if the term with ID stored in the $child_term_id variable is a child of the term with ID stored in the $parent_term_id variable in the taxonomy specified in the $taxonomy variable. If it is, it outputs a message saying so, otherwise it outputs a message saying it is not.

How to Loop Through an Array of Terms to Find Their Ancestors

$terms = get_terms('category');
$parent_term_id = 5;

foreach($terms as $term) {
 if(term_is_ancestor_of($parent_term_id, $term->term_id, 'category')) {
 echo '<p>The term with ID ' . $term->term_id . ' is a child of the term with ID ' . $parent_term_id . '.</p>';
 }
}

This code snippet retrieves all terms from the ‘category’ taxonomy and checks if each one is a child of the term with ID stored in the $parent_term_id variable. If a term is a child of the specified term, it outputs a message saying so.

How to Use term_is_ancestor_of in a Conditional Statement

$parent_term = get_term_by('name', 'Parent', 'category');
$child_term = get_term_by('name', 'Child', 'category');

if(term_is_ancestor_of($parent_term->term_id, $child_term->term_id, 'category')) {
 echo '<p>The term ' . $child_term->name . ' is a child of the term ' . $parent_term->name . '.</p>';
} else {
 echo '<p>The term ' . $child_term->name . ' is not a child of the term ' . $parent_term->name . '.</p>';
}

This code snippet retrieves a parent term and a child term from the ‘category’ taxonomy by their names, and checks if the child term is indeed a child of the parent term. It outputs a message indicating whether the child term is a child of the parent term.

Conclusion

The term_is_ancestor_of function in WordPress is a powerful tool that is used to determine if a term is an ancestor of another term in a hierarchical taxonomy. This function can be employed in a variety of scenarios, such as creating nested lists of terms or displaying hierarchical relationships between terms. By checking whether one term is an ancestor of another, developers can create more complex and organized structures within their WordPress sites, enhancing the overall user experience.

Related WordPress Functions