get_term_by()
Gets the specified term (taxonomy element) by: name, slug, or term ID.
Important: $value is not sanitized when $field=name, you need to do this yourself.
The function always returns the first term that matches the conditions: $field, $value, $taxonomy. But when $field=name, there may be multiple terms with the same names, in which case the function will return only the first found term; if all found terms are needed, use the function get_terms().
Uses: sanitize_term() — cleans the element based on the filter rules specified in the $filter parameter.
No Hooks.
Returns
WP_Term|Array|false.
-
WP_Term|Array— A database row in the specified format (array/object). The array or object will contain the following data:object(WP_Term) { ["term_id"] => int(134) ["name"] => string(4) "AJAX" ["slug"] => string(4) "ajax" ["term_group"] => int(0) ["term_taxonomy_id"] => int(134) ["taxonomy"] => string(8) "post_tag" ["description"] => string(0) "" ["parent"] => int(0) ["count"] => int(4) ["filter"] => string(3) "raw" ["term_order"] => string(1) "0" } false— when the taxonomy does not exist or the term is not found in it.
Usage
get_term_by( $field, $value, $taxonomy, $output, $filter );
- $field(string) (required)
The field in the DB by which the parameter $value will be searched, can be:
slug- by alternative name.name- by name.term_taxonomy_id- by the term taxonomy ID.id- by term ID. Since WP 5.5, the key can also beID.
- $value(string/number) (required)
- The value to find.
- $taxonomy(string) (required)
The name of the taxonomy, category, post_tag, or the name of a custom taxonomy.
Since version 4.4, this parameter is optional if
term_taxonomy_idis specified in $field.- $output(string)
The type of array to return data:
- OBJECT - object, converts to WP_Term object.
- ARRAY_A - associative array;
- ARRAY_N - indexed array.
Default: OBJECT
- $filter(string)
The type of filtering. One of the values:
raw- simply returns the valueedit- filter esc_html() if this field is description, and filter esc_attr() if another field.dbslugrssattribute- filter esc_attr()js- filter esc_js()
See the $context parameter in the function sanitize_term_field(). Or look into the function code.
Default: 'raw'
Examples
#1 Examples of how to get terms from different taxonomies
Examples to get terms by name and taxonomy type (taxonomy_name as category, post_tag or custom taxonomy).
// Let's get the "News" category by its name:
$term = get_term_by('name', 'News', 'category' );
// Get tags (post_tag taxonomy) by slug
$term = get_term_by( 'slug', 'my-tag-name', 'post_tag' );
// Get the term "News" from the taxonomy "my_custom_taxonomy" by its name:
$term = get_term_by( 'name', 'News', 'my_custom_taxonomy' );
// Retrieve a term by its id (term_id) from the taxonomy `category`.
get_term_by( 'id', 12, 'category' )
It is better to use the get_term() function to get the term by ID.
As a result of these examples, the variable $term will contain such an object:
stdClass Object ( [term_id] => 80 [name] => My Category [slug] => my-category [term_group] => 0 [term_taxonomy_id] => 87 [taxonomy] => my_tax [description] => [parent] => 0 [count] => 5 )
#2 Obtaining the post category
This example shows how to get all the data of the first post category, using this function:
global $post;
// get all categories of the current post
$post_cats = get_the_category( $post->ID );
// collect data about all post categories in $cats_data
foreach ( $post_cats as $post_cat ) {
$cats_data[] = get_term_by( 'id', $post_cat->cat_ID, 'category' );
}
// here $cats_data contains the data of all post categories
The first category of the post can be obtained as follows:
$first_cat = get_term_by( 'id', $post_cats[0]->cat_ID, 'category' );
This is a demo example. The idea is that get_the_category() returns an array with all category data and, for example, the first category data will be in the first element of the array: $post_cats[0].
Because of core changes from v4.1 – 4.3, it’s now possible for multiple terms to match the supplied name or slug parameters.
The WP_Term Object returned will be the first matching term found by mySQL, there is no indication that other matching terms may exist. If there is any possibility of multiple terms having the same name or slug in your application, you should use get_terms() instead of get_term_by().
#4 Get custom taxonomy term
If you are using a custom taxonomy, this might not work unless you add the third parameter - taxonomy name:
My custom taxonomy is wpdocs_playlist.
$playlist = get_term_by( 'slug', $playlist_key, 'wpdocs_playlist' );
Notes
- See: sanitize_term_field() The
$contextparam lists the available values for get_term_by()$filterparam.
Changelog
| Since 2.3.0 | Introduced. |
| Since 4.4.0 | $taxonomy is optional if $field is 'term_taxonomy_id'. Converted to return a WP_Term object if $output is OBJECT. |
| Since 5.5.0 | Added 'ID' as an alias of 'id' for the $field parameter. |