get_term( int|WP_Term|object $term, string $taxonomy = '', string $output = OBJECT, string $filter = 'raw' ): WP_Term|array|WP_Error|null
- Since
- 2.3.0, 4.4.0
- Source
wp-includes/taxonomy.php:977
Retrieves a single term's data by numeric ID, WP_Term instance, or a raw database row object, and normalizes the result into a WP_Term object, array, or error. It checks that any supplied taxonomy is registered before doing the lookup, and it runs the result through the 'get_term' and 'get_{$taxonomy}' filters. Reach for get_term_by() instead when you only have a slug or name to search with.
Description
The usage of the get_term function is to apply filters to a term object. It is possible to get a term object from the database before applying the filters.
$term ID must be part of $taxonomy, to get from the database. Failure, might be able to be captured by the hooks. Failure would be the same value as $wpdb returns for the get_row method.
There are two hooks, one is specifically for each term, named 'getterm', and the second is for the taxonomy name, 'term$taxonomy'. Both hooks gets the term object, and the taxonomy name as parameters. Both hooks are expected to return a term object.
'get_term' hook - Takes two parameters the term Object and the taxonomy name.
Must return term object. Used in get_term() as a catch-all filter for every $term.
'get_$taxonomy' hook - Takes two parameters the term Object and the taxonomy name. Must return term object. $taxonomy will be the taxonomy name, so for example, if 'category', it would be 'get_category' as the filter name. Useful for custom taxonomies or plugging into default taxonomies.
Compatibility
- WordPress
- since 4.4.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|WP_Term|object- If integer, term data will be fetched from the database, or from the cache if available.
If stdClass object (as in the results of a database query), will apply filters and return aWP_Termobject with the$termdata.
IfWP_Term, will return$term. $taxonomystringoptional- Taxonomy name that
$termis part of.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|WP_Error|null- WP_Term instance (or array) on success, depending on the
$outputvalue.
WP_Error if$taxonomydoes not exist. Null for miscellaneous failure.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Get a category by ID and return it as an associative array
The "news" category already exists in this install, so look up its ID first and then re-fetch it through get_term() with ARRAY_A output.
$news = get_term_by( 'slug', 'news', 'category' );
if ( $news ) {
$term_array = get_term( $news->term_id, 'category', ARRAY_A );
echo '<pre>' . esc_html( print_r( $term_array, true ) ) . '</pre>';
} else {
echo esc_html( 'Category "news" was not found.' );
}Tell apart a bad taxonomy name from a term that doesn't belong to that taxonomy
The "featured" tag exists by default, so its ID is used to trigger the two different failure paths get_term() can take.
$featured = get_term_by( 'slug', 'featured', 'post_tag' );
// An unregistered taxonomy name makes get_term() return a WP_Error.
$bad_taxonomy = get_term( $featured->term_id, 'not_a_real_taxonomy' );
// 'category' is a real, registered taxonomy, but this term isn't in it,
// so get_term() returns null instead of an error.
$wrong_taxonomy = get_term( $featured->term_id, 'category' );
echo esc_html( 'Unregistered taxonomy result: ' . ( is_wp_error( $bad_taxonomy ) ? $bad_taxonomy->get_error_message() : 'not an error' ) ) . '<br>';
echo esc_html( 'Mismatched taxonomy result: ' . ( is_null( $wrong_taxonomy ) ? 'null' : 'unexpected value' ) );Common problems and fixes · 4
- Why do I get null sometimes and a WP_Error other times?
- Why does get_term() fail even though the taxonomy is definitely registered?
- Why does passing 0 or an empty string as the term always fail?
- Why does the returned term contain unescaped HTML in the name or description?
Why do I get null sometimes and a WP_Error other times?
elseif ( ! $_term ) return null;.Why does get_term() fail even though the taxonomy is definitely registered?
Why does passing 0 or an empty string as the term always fail?
if ( empty( $term ) ) return new WP_Error( 'invalid_term', ... );. A term ID of 0, an empty string, or false will always be treated as invalid input, not as "term not found".Why does the returned term contain unescaped HTML in the name or description?
Alternatives and related functions
get_term_by- When you have a term's slug, name, or another field instead of its numeric ID.
get_terms- When you need a list of multiple terms matching query arguments rather than a single known ID.
get_the_terms- When you need the terms attached to a specific post in a given taxonomy.
sanitize_term_field- When you need to sanitize a single term field for a particular context without re-fetching the whole term.
Performance profile
How much work a call to get_term() 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
- 14–74
- Plugin surface
- 2 hooks
- Called by
- 50
Reaches the database via get_term().
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 111.
Third-party callbacks on 'get_term', 'get_{$taxonomy}' run inside this call, and their cost is not bounded by anything here.
50 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_term()this function does it - hookthird-party callbacks
apply_filters()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 · 44 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_term() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($term) | 14 | __() |
!empty($term) && $term instanceof | 16–17 | is_wp_error() |
!empty($term) && !taxonomy_exists() | 19 | taxonomy_exists(), __() |
!empty($term) && taxonomy_exists() && $term instanceof | 20–21 | taxonomy_exists(), is_wp_error() |
!empty($term) && !($term instanceof) | 21–29 | ::WP_Term(), is_wp_error() |
!empty($term) && taxonomy_exists() && !($term instanceof) | 25–33 | taxonomy_exists(), ::WP_Term(), is_wp_error() |
!empty($term) && !($term instanceof) && is_object($term) | 29–33 | sanitize_term(), is_wp_error() |
!empty($term) && taxonomy_exists() && !($term instanceof) && is_object($term) | 33–37 | taxonomy_exists(), sanitize_term(), is_wp_error() |
!empty($term) && $term instanceof && !is_wp_error() | 35–46 | is_wp_error(), apply_filters(), apply_filters() |
!empty($term) && taxonomy_exists() && $term instanceof && !is_wp_error() | 39–50 | taxonomy_exists(), is_wp_error(), apply_filters(), apply_filters() |
!empty($term) && !($term instanceof) && !is_wp_error() | 40–58 | ::WP_Term(), is_wp_error(), apply_filters(), apply_filters() |
!empty($term) && taxonomy_exists() && !($term instanceof) && !is_wp_error() | 44–62 | taxonomy_exists(), ::WP_Term(), is_wp_error(), apply_filters(), apply_filters() |
32 further outcomes, up to 74 instructions
!empty($term) && $term instanceof && !is_wp_error() && $_term instanceof && $_term === false && $filter === false && $output === false | 45 | is_wp_error(), apply_filters(), apply_filters(), ->to_array() |
!empty($term) && $term instanceof && !is_wp_error() && $_term instanceof && $output === false | 45–48 | is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array() |
!empty($term) && $term instanceof && !is_wp_error() && $_term instanceof && $output !== false | 46–49 | is_wp_error(), apply_filters(), apply_filters(), ->filter() |
!empty($term) && !($term instanceof) && is_object($term) && !is_wp_error() | 48–62 | sanitize_term(), is_wp_error(), apply_filters(), apply_filters() |
!empty($term) && taxonomy_exists() && $term instanceof && !is_wp_error() && $_term instanceof && $_term === false && $filter === false && $output === false | 49 | taxonomy_exists(), is_wp_error(), apply_filters(), apply_filters(), ->to_array() |
!empty($term) && taxonomy_exists() && $term instanceof && !is_wp_error() && $_term instanceof && $output === false | 49–52 | taxonomy_exists(), is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array() |
!empty($term) && !($term instanceof) && !is_wp_error() && $_term instanceof && $_term === false && $filter === false && $output === false | 50–57 | ::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->to_array() |
!empty($term) && taxonomy_exists() && $term instanceof && !is_wp_error() && $_term instanceof && $output !== false | 50–53 | taxonomy_exists(), is_wp_error(), apply_filters(), apply_filters(), ->filter() |
!empty($term) && !($term instanceof) && !is_wp_error() && $_term instanceof && $output === false | 50–60 | ::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array() |
!empty($term) && !($term instanceof) && !is_wp_error() && $_term instanceof && $output !== false | 51–61 | ::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->filter() |
!empty($term) && $term instanceof && !is_wp_error() && $_term instanceof && $_term === false && $filter === false | 51 | is_wp_error(), apply_filters(), apply_filters(), ->to_array(), array_values() |
!empty($term) && $term instanceof && !is_wp_error() && $_term instanceof | 51–54 | is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array(), array_values() |
!empty($term) && taxonomy_exists() && !($term instanceof) && is_object($term) && !is_wp_error() | 52–66 | taxonomy_exists(), sanitize_term(), is_wp_error(), apply_filters(), apply_filters() |
!empty($term) && taxonomy_exists() && !($term instanceof) && !is_wp_error() && $_term instanceof && $_term === false && $filter === false && $output === false | 54–61 | taxonomy_exists(), ::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->to_array() |
!empty($term) && taxonomy_exists() && !($term instanceof) && !is_wp_error() && $_term instanceof && $output === false | 54–64 | taxonomy_exists(), ::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array() |
!empty($term) && taxonomy_exists() && !($term instanceof) && !is_wp_error() && $_term instanceof && $output !== false | 55–65 | taxonomy_exists(), ::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->filter() |
!empty($term) && taxonomy_exists() && $term instanceof && !is_wp_error() && $_term instanceof && $_term === false && $filter === false | 55 | taxonomy_exists(), is_wp_error(), apply_filters(), apply_filters(), ->to_array(), array_values() |
!empty($term) && taxonomy_exists() && $term instanceof && !is_wp_error() && $_term instanceof | 55–58 | taxonomy_exists(), is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array(), array_values() |
!empty($term) && !($term instanceof) && !is_wp_error() && $_term instanceof && $_term === false && $filter === false | 56–63 | ::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->to_array(), array_values() |
!empty($term) && !($term instanceof) && !is_wp_error() && $_term instanceof | 56–66 | ::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array(), array_values() |
!empty($term) && !($term instanceof) && is_object($term) && !is_wp_error() && $_term instanceof && $_term === false && $filter === false && $output === false | 58–61 | sanitize_term(), is_wp_error(), apply_filters(), apply_filters(), ->to_array() |
!empty($term) && !($term instanceof) && is_object($term) && !is_wp_error() && $_term instanceof && $output === false | 58–64 | sanitize_term(), is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array() |
!empty($term) && !($term instanceof) && is_object($term) && !is_wp_error() && $_term instanceof && $output !== false | 59–65 | sanitize_term(), is_wp_error(), apply_filters(), apply_filters(), ->filter() |
!empty($term) && taxonomy_exists() && !($term instanceof) && !is_wp_error() && $_term instanceof && $_term === false && $filter === false | 60–67 | taxonomy_exists(), ::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->to_array(), array_values() |
!empty($term) && taxonomy_exists() && !($term instanceof) && !is_wp_error() && $_term instanceof | 60–70 | taxonomy_exists(), ::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array(), array_values() |
!empty($term) && taxonomy_exists() && !($term instanceof) && is_object($term) && !is_wp_error() && $_term instanceof && $_term === false && $filter === false && $output === false | 62–65 | taxonomy_exists(), sanitize_term(), is_wp_error(), apply_filters(), apply_filters(), ->to_array() |
!empty($term) && taxonomy_exists() && !($term instanceof) && is_object($term) && !is_wp_error() && $_term instanceof && $output === false | 62–68 | taxonomy_exists(), sanitize_term(), is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array() |
!empty($term) && taxonomy_exists() && !($term instanceof) && is_object($term) && !is_wp_error() && $_term instanceof && $output !== false | 63–69 | taxonomy_exists(), sanitize_term(), is_wp_error(), apply_filters(), apply_filters(), ->filter() |
!empty($term) && !($term instanceof) && is_object($term) && !is_wp_error() && $_term instanceof && $_term === false && $filter === false | 64–67 | sanitize_term(), is_wp_error(), apply_filters(), apply_filters(), ->to_array(), array_values() |
!empty($term) && !($term instanceof) && is_object($term) && !is_wp_error() && $_term instanceof | 64–70 | sanitize_term(), is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array(), array_values() |
!empty($term) && taxonomy_exists() && !($term instanceof) && is_object($term) && !is_wp_error() && $_term instanceof && $_term === false && $filter === false | 68–71 | taxonomy_exists(), sanitize_term(), is_wp_error(), apply_filters(), apply_filters(), ->to_array(), array_values() |
!empty($term) && taxonomy_exists() && !($term instanceof) && is_object($term) && !is_wp_error() && $_term instanceof | 68–74 | taxonomy_exists(), sanitize_term(), is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array(), array_values() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 111 instructions, 14–74 executed per call, 14 branches. The work does not change between versions.
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 · 2
2 hooks fire while get_term() runs, in this order:
Uses · 8
- __()Retrieves the translation of $text.
- taxonomy_exists()Determines whether the taxonomy name exists.
- sanitize_term()Sanitizes all term fields.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_Error::__construct()Initializes the error.
- WP_Term::__construct()Constructor.
- WP_Term::get_instance()Retrieve WP_Term instance.
Used by · 50
- WP_Links_List_Table::column_categories()Handles the link categories column output.
- WP_Query::get_queried_object()Retrieves the currently queried object.
- WP_REST_Menu_Items_Controller::prepare_item_for_database()Prepares a single nav menu item for create or update.
- WP_REST_Menus_Controller::update_item()Updates a single term from a taxonomy.
- WP_REST_Posts_Controller::check_assign_terms_permission()Checks whether current user can assign all terms sent with the current request.
- WP_REST_Term_Search_Handler::prepare_item()Prepares the search result for a given term ID.
- WP_REST_Term_Search_Handler::prepare_item_links()Prepares links for the search result of a given ID.
- WP_REST_Terms_Controller::create_item()Creates a single term in a taxonomy.
- WP_REST_Terms_Controller::get_term()Get the term, if the ID is valid.
- WP_REST_Terms_Controller::prepare_item_for_database()Prepares a single term for create or update.
- WP_REST_Terms_Controller::prepare_links()Prepares links for the request.
- WP_REST_Terms_Controller::update_item()Updates a single term from a taxonomy.
Show all 50
- WP_Term_Query::get_terms()Retrieves the query results.
- WP_Term_Query::populate_terms()Creates an array of term objects from an array of term IDs.
- WP_Terms_List_Table::_rows()
- WP_Terms_List_Table::column_name()
- Walker_Category::start_el()Starts the element output.
- Walker_Nav_Menu_Edit::start_el()Start the element output.
- _get_term_children()Gets the subset of $terms that are descendants of $term_id.
- _wp_ajax_add_hierarchical_term()Handles adding a hierarchical term via AJAX.
- _wp_ajax_menu_quick_search()Prints the appropriate response to a menu quick search.
- edit_term_link()Displays or retrieves the edit term link with formatting.
- export_wp()Generates the WXR export file for download.
- get_ancestors()Gets an array of ancestor IDs for a given object.
- get_cat_name()Retrieves the name of a category from its ID.
- get_category()Retrieves category data given a category ID or category object.
- get_category_by_path()Retrieves a category based on URL containing the category slug.
- get_category_to_edit()Gets category object for given ID and 'edit' filter context.
- get_edit_term_link()Retrieves the URL for editing a given term.
- get_object_subtype()Returns the object subtype for a given object ID of a specific type.
- get_object_term_cache()Retrieves the cached term objects for the given object ID.
- get_permalink()Retrieves the full permalink for the current post or post ID.
- get_tag()Retrieves a post tag by tag ID or tag object.
- get_term_by()Gets all term data from database by term field and data.
- get_term_feed_link()Retrieves the feed link for a term.
- get_term_field()Gets sanitized term field.
- get_term_link()Generates a permalink for a taxonomy term archive.
- get_term_parents_list()Retrieves term parents with separator.
- get_term_to_edit()Sanitizes term for editing.
- get_the_category_by_ID()Retrieves category name based on category ID.
- is_term_publicly_viewable()Determines whether a term is publicly viewable.
- map_meta_cap()Maps a capability to the primitive capabilities required of the given user to satisfy the capability being checked.
- rest_get_route_for_term()Gets the REST API route for a term.
- term_is_ancestor_of()Checks if a term is an ancestor of another term.
- wp_ajax_add_menu_item()Handles adding a menu item via AJAX.
- wp_ajax_add_tag()Handles adding a tag via AJAX.
- wp_ajax_delete_tag()Handles deleting a tag via AJAX.
- wp_ajax_inline_save_tax()Handles Quick Edit saving for a term via AJAX.
- wp_delete_term()Removes a term from the database.
- wp_get_nav_menu_object()Returns a navigation menu object.
Source code
function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { if ( empty( $term ) ) { return new WP_Error( 'invalid_term', __( 'Empty Term.' ) ); } if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) { return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); } if ( $term instanceof WP_Term ) { $_term = $term; } elseif ( is_object( $term ) ) { if ( empty( $term->filter ) || 'raw' === $term->filter ) { $_term = sanitize_term( $term, $taxonomy, 'raw' ); $_term = new WP_Term( $_term ); } else { $_term = WP_Term::get_instance( $term->term_id ); } } else { $_term = WP_Term::get_instance( $term, $taxonomy ); } if ( is_wp_error( $_term ) ) { return $_term; } elseif ( ! $_term ) { return null; } // Ensure for filters that this is not empty. $taxonomy = $_term->taxonomy; $old_term = $_term; /** * Filters a taxonomy term object. * * The {@see 'get_$taxonomy'} hook is also available for targeting a specific * taxonomy. * * @since 2.3.0 * @since 4.4.0 `$_term` is now a `WP_Term` object. * * @param WP_Term $_term Term object. * @param string $taxonomy The taxonomy slug. */ $_term = apply_filters( 'get_term', $_term, $taxonomy ); /** * Filters a taxonomy term object. * * The dynamic portion of the hook name, `$taxonomy`, refers * to the slug of the term's taxonomy. * * Possible hook names include: * * - `get_category` * - `get_post_tag` * * @since 2.3.0 * @since 4.4.0 `$_term` is now a `WP_Term` object. * * @param WP_Term $_term Term object. * @param string $taxonomy The taxonomy slug. */ $_term = apply_filters( "get_{$taxonomy}", $_term, $taxonomy ); // Bail if a filter callback has changed the type of the `$_term` object. if ( ! ( $_term instanceof WP_Term ) ) { return $_term; } // Sanitize term, according to the specified filter. if ( $_term !== $old_term || $_term->filter !== $filter ) { $_term->filter( $filter ); } if ( ARRAY_A === $output ) { return $_term->to_array(); } elseif ( ARRAY_N === $output ) { return array_values( $_term->to_array() ); }Changelog
Introduced in 2.3.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$output is OBJECT.The
$taxonomy parameter was made optional.from the docblockAbout 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.