term_exists( int|string $term, string $taxonomy = '', int $parent_term = null ): mixed
- Since
- 3.0.0, 6.0.0
- Source
wp-includes/taxonomy.php:1575
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
- Scaling
- Constant
- Instructions
- 7–88
- Plugin surface
- 1 hook
- Called by
- 13
Reaches the database via get_terms().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 108.
Third-party callbacks on 'term_exists_default_query_args' run inside this call, and their cost is not bounded by anything here.
13 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
$term === null | 7 | none |
$term !== null && is_int($term) && $term === 0 | 24–29 | apply_filters() |
$term !== null && !is_int($term) && $term === "" | 29–34 | apply_filters(), wp_unslash() |
$term !== null && is_int($term) && $term !== 0 && empty($terms) | 38–43 | apply_filters(), include(), get_terms() |
$term !== null && is_int($term) && $term !== 0 && !empty($terms) && is_wp_error() | 42–47 | apply_filters(), include(), get_terms(), is_wp_error() |
$term !== null && is_int($term) && $term !== 0 && !empty($terms) && !is_wp_error() | 49–59 | apply_filters(), include(), get_terms(), is_wp_error(), array_shift() |
$term !== null && !is_int($term) && $term !== "" && !is_wp_error() | 52–62 | apply_filters(), wp_unslash(), sanitize_title(), slug(), get_terms(), is_wp_error() |
$term !== null && !is_int($term) && $term !== "" && !empty($terms) | 56–66 | apply_filters(), wp_unslash(), sanitize_title(), slug(), get_terms(), is_wp_error(), is_wp_error() |
$term !== null && !is_int($term) && $term !== "" && empty($terms) | 58–68 | apply_filters(), wp_unslash(), sanitize_title(), slug(), get_terms(), name(), get_terms() |
$term !== null && !is_int($term) && $term !== "" && is_wp_error() | 62–72 | apply_filters(), wp_unslash(), sanitize_title(), slug(), get_terms(), name(), get_terms(), is_wp_error() |
$term !== null && !is_int($term) && $term !== "" && is_wp_error() | 62–72 | apply_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–78 | apply_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–76 | apply_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–84 | apply_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–88 | apply_filters(), wp_unslash(), sanitize_title(), slug(), get_terms(), is_wp_error(), name(), get_terms(), is_wp_error(), array_shift() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 108 | 7–88 | 13 | |
| 8.5 | 108 | 7–88 | 13 | |
| 8.4 | 108 | 7–88 | 13 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 112 | 7–92 | 13 | |
| 8.2 | 112 | 7–92 | 13 | |
| 8.1 | 112 | 7–92 | 13 | |
| 7.4 | 112 | 7–92 | 13 |
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:
- apply_filters( term_exists_default_query_args )filterline 1614 (+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
- category_exists()Checks whether a category exists.
- export_wp()Generates the WXR export file for download.
- is_term()Check if Term exists.
- register_taxonomy()Creates or modifies a taxonomy object.
- tag_exists()Checks whether a post tag with a given name exists.
- wp_create_term()Adds a new term to the database if it does not already exist.
- wp_delete_term()Removes a term from the database.
- wp_insert_category()Updates an existing Category or creates a new Category.
- wp_insert_term()Adds a new term to the database.
- wp_remove_object_terms()Removes term(s) associated with a given object.
- wp_set_object_terms()Creates term and taxonomy relationships.
- wp_unique_term_slug()Makes term slug unique, if it isn't already.
Show all 13
- wp_update_term()Updates term based on arguments provided.
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.
Signature, return type and hooks compared across 5 parsed releases.
$term retyped from int|string to int|string|null.verified against sourceget_terms().from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.