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.
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
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.
1099functionget_term_by($field,$value,$taxonomy='',$output=OBJECT,$filter='raw'){11001101// 'term_taxonomy_id' lookups don't require taxonomy checks.1102if('term_taxonomy_id'!==$field&&!taxonomy_exists($taxonomy)){1103returnfalse;1104}11051106// No need to perform a query for empty 'slug' or 'name'.1107if('slug'===$field||'name'===$field){1108$value=(string)$value;11091110if(0===strlen($value)){1111returnfalse;1112}1113}11141115if('id'===$field||'ID'===$field||'term_id'===$field){1116$term=get_term((int)$value,$taxonomy,$output,$filter);1117if(is_wp_error($term)||null===$term){1118$term=false;1119}1120return$term;1121}11221123$args=array(1124'get'=>'all',1125'number'=>1,1126'taxonomy'=>$taxonomy,1127'update_term_meta_cache'=>false,1128'orderby'=>'none',1129'suppress_filter'=>true,1130);11311132switch($field){1133case'slug':1134$args['slug']=$value;1135break;1136case'name':1137$args['name']=$value;1138break;1139case'term_taxonomy_id':1140$args['term_taxonomy_id']=$value;1141unset($args['taxonomy']);1142break;1143default:1144returnfalse;1145}11461147$terms=get_terms($args);1148if(is_wp_error($terms)||empty($terms)){1149returnfalse;1150}11511152$term=array_shift($terms);11531154// In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the DB.1155if('term_taxonomy_id'===$field){1156$taxonomy=$term->taxonomy;1157}11581159returnget_term($term,$taxonomy,$output,$filter);1160}
History
Introduced in 2.3.0. Unchanged from 6.7.7 through 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.