wppaste
WordPress

get_term_by( string $field, string|int $value, string $taxonomy = '', string $output = OBJECT, string $filter = 'raw' ): WP_Term|array|false

Since
2.3.0, 4.4.0, 5.5.0
Source
wp-includes/taxonomy.php:1099
Gets all term data from database by term field and data.

Description

Warning: $value is not escaped for 'name' $field. You must do it yourself, if required.

The default $field is 'id', therefore it is possible to also use null for field, but not recommended that you do so.

If $value does not exist, the return value will be false. If $taxonomy exists and $field and $value combinations exist, the term will be returned.

This function will always return the first term that matches the $field- $value-$taxonomy combination specified in the parameters. If your query is likely to match more than one term (as is likely to be the case when $field is 'name', for example), consider using get_terms() instead; that way, you will get all matching terms, and can provide your own logic for deciding which one was intended.

Compatibility

WordPress
since 5.5.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

$fieldstring
Either 'slug', 'name', 'term_id' (or 'id', 'ID'), or 'term_taxonomy_id'.
$valuestring|int
Search for this term value.
$taxonomystringoptional
Taxonomy name. Optional, if $field is 'term_taxonomy_id'.Default: ''
$outputstringoptional
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.Default: OBJECT
$filterstringoptional
How to sanitize term fields. Default 'raw'.Default: 'raw'

Return value

WP_Term|array|false
WP_Term instance (or array) on success, depending on the $output value.
False if $taxonomy does not exist or $term was not found.

Performance profile

How much work a call to get_term_by() 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_term().

Scaling
Constant

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

Instructions
12–69

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
20

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

What it touches

  • querycontent queryget_term()called directly
  • hookthird-party callbacksapply_filters()one call below get_term_by()

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 · 8 distinct outcomes

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

WhenInstructionsCalls it makes
$field !== "term_taxonomy_id"12–41taxonomy_exists()
$field === "term_taxonomy_id"15–37none
$field === "term_taxonomy_id"27–38get_term(), is_wp_error()
$field !== "term_taxonomy_id" && taxonomy_exists()31–42taxonomy_exists(), get_term(), is_wp_error()
$field === "term_taxonomy_id" && $field !== "id" && $field !== "ID" && $field !== "term_id"37–51get_terms(), is_wp_error()
$field !== "term_taxonomy_id" && taxonomy_exists() && $field !== "id" && $field !== "ID" && $field !== "term_id"41–55taxonomy_exists(), get_terms(), is_wp_error()
$field === "term_taxonomy_id" && $field !== "id" && $field !== "ID" && $field !== "term_id" && !is_wp_error() && !empty($terms)51–65get_terms(), is_wp_error(), array_shift(), get_term()
$field !== "term_taxonomy_id" && taxonomy_exists() && $field !== "id" && $field !== "ID" && $field !== "term_id" && !is_wp_error() && !empty($terms)55–69taxonomy_exists(), get_terms(), is_wp_error(), array_shift(), get_term()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9612–6917
8.59612–6917
8.49612–6917
8.39612–6917
8.29612–6917
8.19612–69171 more instruction than PHP 7.4
7.49512–6917

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.

Uses · 4

  • taxonomy_exists()Determines whether the taxonomy name exists.
  • get_term()Gets all term data from database by term ID.
  • is_wp_error()Checks whether the given variable is a WordPress Error.
  • get_terms()Retrieves the terms in a given taxonomy or list of taxonomies.

Used by · 20

Show all 20

Source code

function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { 	// 'term_taxonomy_id' lookups don't require taxonomy checks.	if ( 'term_taxonomy_id' !== $field && ! taxonomy_exists( $taxonomy ) ) {		return false;	} 	// No need to perform a query for empty 'slug' or 'name'.	if ( 'slug' === $field || 'name' === $field ) {		$value = (string) $value; 		if ( 0 === strlen( $value ) ) {			return false;		}	} 	if ( 'id' === $field || 'ID' === $field || 'term_id' === $field ) {		$term = get_term( (int) $value, $taxonomy, $output, $filter );		if ( is_wp_error( $term ) || null === $term ) {			$term = false;		}		return $term;	} 	$args = array(		'get'                    => 'all',		'number'                 => 1,		'taxonomy'               => $taxonomy,		'update_term_meta_cache' => false,		'orderby'                => 'none',		'suppress_filter'        => true,	); 	switch ( $field ) {		case 'slug':			$args['slug'] = $value;			break;		case 'name':			$args['name'] = $value;			break;		case 'term_taxonomy_id':			$args['term_taxonomy_id'] = $value;			unset( $args['taxonomy'] );			break;		default:			return false;	} 	$terms = get_terms( $args );	if ( is_wp_error( $terms ) || empty( $terms ) ) {		return false;	} 	$term = array_shift( $terms ); 	// In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the DB.	if ( 'term_taxonomy_id' === $field ) {		$taxonomy = $term->taxonomy;	} 	return get_term( $term, $taxonomy, $output, $filter );}

Changelog

Introduced in 2.3.0. Unchanged from 6.7.7 through 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.

5.5.0
Added 'ID' as an alias of 'id' for the $field parameter.from the docblock
4.4.0
$taxonomy is optional if $field is 'term_taxonomy_id'. Converted to return a WP_Term object if $output is OBJECT.from the docblock
2.3.0
Introduced.from the docblock

About 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.