wppaste
WordPress

term_exists( int|string $term, string $taxonomy = '', int $parent_term = null ): mixed

Since
3.0.0, 6.0.0
Source
wp-includes/taxonomy.php:1586
Determines whether a taxonomy term exists.

Description

Formerly is_term(), introduced in 2.3.0.

For more information on this and similar theme functions, check out the https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags article in the Theme Developer Handbook.

Compatibility

WordPress
since 6.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$termint|string
The term to check. Accepts term ID, slug, or name.
$taxonomystringoptional
The taxonomy name to use.Default: ''
$parent_termintoptional
ID of parent term under which to confine the exists search.Default: null

Return value

mixed
Returns null if the term does not exist.
Returns the term ID if no taxonomy is specified and the term ID exists.
Returns an array of the term ID and the term taxonomy ID if the taxonomy is specified and the pairing exists.
Returns 0 if term ID 0 is passed to the function.

Performance profile

How much work a call to term_exists() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_terms().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
7–88

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 108.

Plugin surface
1 hook

Third-party callbacks on 'term_exists_default_query_args' run inside this call, and their cost is not bounded by anything here.

Called by
13

13 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_terms()called directly

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 15 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost term_exists() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$term === null7none
$term !== null && is_int($term) && $term === 024–29apply_filters()
$term !== null && !is_int($term) && $term === ""29–34apply_filters(), wp_unslash()
$term !== null && is_int($term) && $term !== 0 && empty($terms)38–43apply_filters(), include(), get_terms()
$term !== null && is_int($term) && $term !== 0 && !empty($terms) && is_wp_error()42–47apply_filters(), include(), get_terms(), is_wp_error()
$term !== null && is_int($term) && $term !== 0 && !empty($terms) && !is_wp_error()49–59apply_filters(), include(), get_terms(), is_wp_error(), array_shift()
$term !== null && !is_int($term) && $term !== "" && !is_wp_error()52–62apply_filters(), wp_unslash(), sanitize_title(), slug(), get_terms(), is_wp_error()
$term !== null && !is_int($term) && $term !== "" && !empty($terms)56–66apply_filters(), wp_unslash(), sanitize_title(), slug(), get_terms(), is_wp_error(), is_wp_error()
$term !== null && !is_int($term) && $term !== "" && empty($terms)58–68apply_filters(), wp_unslash(), sanitize_title(), slug(), get_terms(), name(), get_terms()
$term !== null && !is_int($term) && $term !== "" && is_wp_error()62–72apply_filters(), wp_unslash(), sanitize_title(), slug(), get_terms(), name(), get_terms(), is_wp_error()
$term !== null && !is_int($term) && $term !== "" && is_wp_error()62–72apply_filters(), wp_unslash(), sanitize_title(), slug(), get_terms(), is_wp_error(), name(), get_terms()
$term !== null && !is_int($term) && $term !== "" && !empty($terms) && !is_wp_error()63–78apply_filters(), wp_unslash(), sanitize_title(), slug(), get_terms(), is_wp_error(), is_wp_error(), array_shift()
3 further outcomes, up to 88 instructions
$term !== null && !is_int($term) && $term !== "" && !empty($terms) && is_wp_error()66–76apply_filters(), wp_unslash(), sanitize_title(), slug(), get_terms(), is_wp_error(), name(), get_terms(), is_wp_error()
$term !== null && !is_int($term) && $term !== "" && !is_wp_error()69–84apply_filters(), wp_unslash(), sanitize_title(), slug(), get_terms(), name(), get_terms(), is_wp_error(), array_shift()
$term !== null && !is_int($term) && $term !== "" && !empty($terms)73–88apply_filters(), wp_unslash(), sanitize_title(), slug(), get_terms(), is_wp_error(), name(), get_terms(), is_wp_error(), array_shift()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1087–8813
8.51087–8813
8.41087–88134 fewer instructions than PHP 8.3
8.31127–9213
8.21127–9213
8.11127–9213
7.41127–9213

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while term_exists() runs, in this order:

  1. apply_filters( term_exists_default_query_args )filterline 1625 (+39 into the body)

    Filters default query arguments for checking if a term exists.

Uses · 6

  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • wp_parse_args()Merges user defined arguments into defaults array.
  • get_terms()Retrieves the terms in a given taxonomy or list of taxonomies.
  • wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
  • sanitize_title()Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
  • is_wp_error()Checks whether the given variable is a WordPress Error.

Used by · 13

Show all 13

Source code

function term_exists( $term, $taxonomy = '', $parent_term = null ) {	global $_wp_suspend_cache_invalidation; 	if ( null === $term ) {		return null;	} 	$defaults = array(		'get'                    => 'all',		'fields'                 => 'ids',		'number'                 => 1,		'update_term_meta_cache' => false,		'order'                  => 'ASC',		'orderby'                => 'term_id',		'suppress_filter'        => true,	); 	// Ensure that while importing, queries are not cached.	if ( ! empty( $_wp_suspend_cache_invalidation ) ) {		$defaults['cache_results'] = false;	} 	if ( ! empty( $taxonomy ) ) {		$defaults['taxonomy'] = $taxonomy;		$defaults['fields']   = 'all';	} 	/**	 * Filters default query arguments for checking if a term exists.	 *	 * @since 6.0.0	 *	 * @param array      $defaults    An array of arguments passed to get_terms().	 * @param int|string $term        The term to check. 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.	 */	$defaults = apply_filters( 'term_exists_default_query_args', $defaults, $term, $taxonomy, $parent_term ); 	if ( is_int( $term ) ) {		if ( 0 === $term ) {			return 0;		}		$args  = wp_parse_args( array( 'include' => array( $term ) ), $defaults );		$terms = get_terms( $args );	} else {		$term = trim( wp_unslash( $term ) );		if ( '' === $term ) {			return null;		} 		if ( ! empty( $taxonomy ) && is_numeric( $parent_term ) ) {			$defaults['parent'] = (int) $parent_term;		} 		$args  = wp_parse_args( array( 'slug' => sanitize_title( $term ) ), $defaults );		$terms = get_terms( $args );		if ( empty( $terms ) || is_wp_error( $terms ) ) {			$args  = wp_parse_args( array( 'name' => $term ), $defaults );			$terms = get_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;}

Changelog

Introduced in 3.0.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.1.0
Parameter $term retyped from int|string to int|string|null.verified against source
6.0.0
Converted to use get_terms().from the docblock
3.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/taxonomy.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.