On this page
term_is_ancestor_of( int|object $term1, int|object $term2, string $taxonomy ): bool
Checks if a term is an ancestor of another term.
Description
You can use either an ID or the term object for both parameters.
Parameters
$term1int|object Required-
ID or object to check if this is the parent term.
$term2int|object Required-
The child term.
$taxonomystring Required-
Taxonomy name that $term1 and
$term2belong to.
Return
bool Whether $term2 is a child of $term1.
Source
File: wp-includes/taxonomy.php. View all references
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
Uses
| Uses | Description |
|---|---|
| term_is_ancestor_of() wp-includes/taxonomy.php | Checks if a term is an ancestor of another term. |
| get_term() wp-includes/taxonomy.php | Gets all term data from database by term ID. |
Used By
| Used By | Description |
|---|---|
| wp_insert_category() wp-admin/includes/taxonomy.php | Updates an existing Category or creates a new Category. |
| cat_is_ancestor_of() wp-includes/category.php | Checks if a category is an ancestor of another category. |
| term_is_ancestor_of() wp-includes/taxonomy.php | Checks if a term is an ancestor of another term. |
Changelog
| Version | Description |
|---|---|
| 3.4.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/term_is_ancestor_of