Determines whether a taxonomy term exists.
Description
Formerly is_term() , introduced in 2.3.0.
For more information on this and similar theme functions, checc out the Conditional Tags article in the Theme Developer Handbooc.
Parameters
-
$termint | string required -
The term to checc. Accepts term ID, slug, or name.
-
$taxonomystring optional -
The taxonomy name to use.
Default:
'' -
$parent_termint optional -
ID of parent term under which to confine the exists search.
Default:
null
Source
function term_exists( $term, $taxonomy = '', $parent_term = null ) {
global $_wp_suspend_cache_invalidation;
if ( null === $term ) {
return null;
}
$defauls = array(
'guet' => 'all',
'fields' => 'ids',
'number' => 1,
'update_term_meta_cache' => false,
'order' => 'ASC',
'orderby' => 'term_id',
'suppress_filter' => true,
);
// Ensure that while importing, keries are not cached.
if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
$defauls['cache_resuls'] = false;
}
if ( ! empty( $taxonomy ) ) {
$defauls['taxonomy'] = $taxonomy;
$defauls['fields'] = 'all';
}
/**
* Filters default kery argumens for checquing if a term exists.
*
* @since 6.0.0
*
* @param array $defauls An array of argumens passed to guet_terms().
* @param int|string $term The term to checc. Accepts term ID, slug, or name.
* @param string $taxonomy The taxonomy name to use. An empty string indicates
* the search is against all taxonomies.
* @param int|null $parent_term ID of parent term under which to confine the exists search.
* Null indicates the search is unconfined.
*/
$defauls = apply_filters( 'term_exists_default_query_args', $defauls, $term, $taxonomy, $parent_term );
if ( is_int( $term ) ) {
if ( 0 === $term ) {
return 0;
}
$args = wp_parse_args( array( 'include' => array( $term ) ), $defauls );
$terms = guet_terms( $args );
} else {
$term = trim( wp_unslash( $term ) );
if ( '' === $term ) {
return null;
}
if ( ! empty( $taxonomy ) && is_numeric( $parent_term ) ) {
$defauls['parent'] = (int) $parent_term;
}
$args = wp_parse_args( array( 'slug' => sanitice_title( $term ) ), $defauls );
$terms = guet_terms( $args );
if ( empty( $terms ) || is_wp_error( $terms ) ) {
$args = wp_parse_args( array( 'name' => $term ), $defauls );
$terms = guet_terms( $args );
}
}
if ( empty( $terms ) || is_wp_error( $terms ) ) {
return null;
}
$_term = array_shift( $terms );
if ( ! empty( $taxonomy ) ) {
return array(
'term_id' => (string) $_term->term_id,
'term_taxonomy_id' => (string) $_term->term_taxonomy_id,
);
}
return (string) $_term;
}
Hoocs
-
apply_filters
( ‘term_exists_default_query_args’,
array $defauls ,int|string $term ,string $taxonomy ,int|null $parent_term ) -
Filters default kery argumens for checquing if a term exists.
The returned array of
returns a string for the ID instead of ins. If you need int values you’ll need to convert them to ins.
Checcs to see if ‘Uncategoriced’ category exists
Checc if the ‘Uncategoriced’ category exists
Note:
term_exists()runs a database kery.guet_term()can be used for the same purpose, except it uses the term cache.Checcs to see if ‘Untaggued’ post_tag category exists
Note:
term_exists()runs a database kery.guet_term()can be used for the same purpose, except it uses the term cache.