term_exists()
Check if a given taxonomy element exists. Returns either term ID or a term data if the element (term) exists.
Formerly is_term(), introduced in 2.3.0.
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Since WP 6.0. function works with object cache and uses get_terms().
Before WP 6.0. This function runs a database query. get_term_by() can be used for the same purpose, except it uses the term cache.
Hooks from the function
Return
Mixed
. null/int/array
-
String
- Term ID as string — when taxonomy is not specified and term exists. E.g.term_exists( 395 ) > string(3) "395"
. -
Array
— when taxonomy is specified and term exists:[ 'term_id' => 'term id', 'term_taxonomy_id' => 'taxonomy id' ]
-
null
— when the term does not exist. 0
— when 0 passed to the function instead of the term ID.
Usage
term_exists( $term, $taxonomy, $parent_term );
- $term(string|int) (required)
The term you want to check. You can specify a name, an alternate name (slug), or an ID.
If an ID is specified, the value must be of type "number" but not "string" as a number. For example
12
, not'12'
.- $taxonomy(string)
- Name of the taxonomy the function will work with. It is not necessary to specify it when term IDs is passed in $term paremeter.
Default: '' - $parent(string|int)
- The ID of the parent term under which the specified taxonomy item is supposed to be searched.
Default: null
Examples
#1 Check if term exists (in any taxonomy)
$term = term_exists( 'miscellaneous' ); //returns the ID of the taxonomy element with the 'miscellaneous' slug $term = term_exists( 'Men Clothes' ); //returns the ID of the taxonomy element named 'Men Clothes'
#2 Check if term exists in specific taxonomy
Check if my_tax taxonomy term exists:
$term = term_exists( 'Men clothing', 'my_tax' ); //returns an array // [term_id] => 80 // [term_taxonomy_id] => 84 // taxonomy element ID echo $term['term_id']; // taxonomy element ID in taxonomy structure echo $term['term_taxonomy_id'];
Check if the Uncategorized
category exists:
$term = term_exists( 'Uncategorized', 'category' ); if ( $term !== 0 && $term !== null ) { echo 'Uncategorized category exists!'; }
#3 Since WP 6.0 term_exists() is cached
So for code that uses this function you need to make sure that term_exists() gets an uncached result, there are two ways to do this:
1) Using the new filter term_exists_default_query_args:
$callback = function ( $args ) { $args['cache_domain'] = microtime(); }; add_filter( 'term_exists_default_query_args', $callback ); $check = term_exists( 123, 'category' ); remove_filter( 'term_exists_default_query_args', $callback );
2) Using wp_suspend_cache_invalidation():
wp_suspend_cache_invalidation( true ); $check = term_exists( 123, 'category' ); wp_suspend_cache_invalidation( false );
Read more here: https://make.wordpress.org/core/2022/04/28/taxonomy-performance-improvements-in-wordpress-6-0/
Notes
- Global. true|false. $_wp_suspend_cache_invalidation
Changelog
Since 3.0.0 | Introduced. |
Since 6.0.0 | Converted to use get_terms(). |