wppaste
WordPress

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.

Gets all term data from database by term ID.

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 a WP_Term object with the $term data.
If WP_Term, will return $term.
$taxonomystringoptional
Taxonomy name that $term is 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 $output value.
WP_Error if $taxonomy does 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?

The source only builds a WP_Error for an empty $term or for a $taxonomy string that fails taxonomy_exists(). Any other lookup failure, such as a valid term ID that doesn't belong to the $taxonomy you passed, falls through to elseif ( ! $_term ) return null;.

Why does get_term() fail even though the taxonomy is definitely registered?

get_term() calls taxonomy_exists( $taxonomy ) only when $taxonomy is non-empty. If your code runs before the taxonomy's register_taxonomy() call has executed for that request, the check fails and you get an invalid_taxonomy WP_Error even though the taxonomy will exist a moment later.

Why does passing 0 or an empty string as the term always fail?

The very first line of the function is 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?

The $filter parameter defaults to 'raw', which returns fields as stored in the database, not sanitized for display. Pass 'display' for output that's safe to echo, or run fields through sanitize_term_field() yourself.

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

Reaches the database via get_term().

Scaling
Constant

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

Instructions
14–74

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

Plugin surface
2 hooks

Third-party callbacks on 'get_term', 'get_{$taxonomy}' run inside this call, and their cost is not bounded by anything here.

Called by
50

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

What it touches

  • querycontent queryget_term()this function does it
  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
empty($term)14__()
!empty($term) && $term instanceof16–17is_wp_error()
!empty($term) && !taxonomy_exists()19taxonomy_exists(), __()
!empty($term) && taxonomy_exists() && $term instanceof20–21taxonomy_exists(), is_wp_error()
!empty($term) && !($term instanceof)21–29::WP_Term(), is_wp_error()
!empty($term) && taxonomy_exists() && !($term instanceof)25–33taxonomy_exists(), ::WP_Term(), is_wp_error()
!empty($term) && !($term instanceof) && is_object($term)29–33sanitize_term(), is_wp_error()
!empty($term) && taxonomy_exists() && !($term instanceof) && is_object($term)33–37taxonomy_exists(), sanitize_term(), is_wp_error()
!empty($term) && $term instanceof && !is_wp_error()35–46is_wp_error(), apply_filters(), apply_filters()
!empty($term) && taxonomy_exists() && $term instanceof && !is_wp_error()39–50taxonomy_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–62taxonomy_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 === false45is_wp_error(), apply_filters(), apply_filters(), ->to_array()
!empty($term) && $term instanceof && !is_wp_error() && $_term instanceof && $output === false45–48is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array()
!empty($term) && $term instanceof && !is_wp_error() && $_term instanceof && $output !== false46–49is_wp_error(), apply_filters(), apply_filters(), ->filter()
!empty($term) && !($term instanceof) && is_object($term) && !is_wp_error()48–62sanitize_term(), is_wp_error(), apply_filters(), apply_filters()
!empty($term) && taxonomy_exists() && $term instanceof && !is_wp_error() && $_term instanceof && $_term === false && $filter === false && $output === false49taxonomy_exists(), is_wp_error(), apply_filters(), apply_filters(), ->to_array()
!empty($term) && taxonomy_exists() && $term instanceof && !is_wp_error() && $_term instanceof && $output === false49–52taxonomy_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 === false50–57::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->to_array()
!empty($term) && taxonomy_exists() && $term instanceof && !is_wp_error() && $_term instanceof && $output !== false50–53taxonomy_exists(), is_wp_error(), apply_filters(), apply_filters(), ->filter()
!empty($term) && !($term instanceof) && !is_wp_error() && $_term instanceof && $output === false50–60::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array()
!empty($term) && !($term instanceof) && !is_wp_error() && $_term instanceof && $output !== false51–61::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->filter()
!empty($term) && $term instanceof && !is_wp_error() && $_term instanceof && $_term === false && $filter === false51is_wp_error(), apply_filters(), apply_filters(), ->to_array(), array_values()
!empty($term) && $term instanceof && !is_wp_error() && $_term instanceof51–54is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array(), array_values()
!empty($term) && taxonomy_exists() && !($term instanceof) && is_object($term) && !is_wp_error()52–66taxonomy_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 === false54–61taxonomy_exists(), ::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->to_array()
!empty($term) && taxonomy_exists() && !($term instanceof) && !is_wp_error() && $_term instanceof && $output === false54–64taxonomy_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 !== false55–65taxonomy_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 === false55taxonomy_exists(), is_wp_error(), apply_filters(), apply_filters(), ->to_array(), array_values()
!empty($term) && taxonomy_exists() && $term instanceof && !is_wp_error() && $_term instanceof55–58taxonomy_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 === false56–63::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->to_array(), array_values()
!empty($term) && !($term instanceof) && !is_wp_error() && $_term instanceof56–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 === false58–61sanitize_term(), is_wp_error(), apply_filters(), apply_filters(), ->to_array()
!empty($term) && !($term instanceof) && is_object($term) && !is_wp_error() && $_term instanceof && $output === false58–64sanitize_term(), is_wp_error(), apply_filters(), apply_filters(), ->filter(), ->to_array()
!empty($term) && !($term instanceof) && is_object($term) && !is_wp_error() && $_term instanceof && $output !== false59–65sanitize_term(), is_wp_error(), apply_filters(), apply_filters(), ->filter()
!empty($term) && taxonomy_exists() && !($term instanceof) && !is_wp_error() && $_term instanceof && $_term === false && $filter === false60–67taxonomy_exists(), ::WP_Term(), is_wp_error(), apply_filters(), apply_filters(), ->to_array(), array_values()
!empty($term) && taxonomy_exists() && !($term instanceof) && !is_wp_error() && $_term instanceof60–70taxonomy_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 === false62–65taxonomy_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 === false62–68taxonomy_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 !== false63–69taxonomy_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 === false64–67sanitize_term(), is_wp_error(), apply_filters(), apply_filters(), ->to_array(), array_values()
!empty($term) && !($term instanceof) && is_object($term) && !is_wp_error() && $_term instanceof64–70sanitize_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 === false68–71taxonomy_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 instanceof68–74taxonomy_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:

  1. apply_filters( get_term )filterline 1021 (+44 into the body)

    Filters a taxonomy term object.

  2. apply_filters( get_{$taxonomy} )filterline 1040 (+63 into the body)

    Filters a taxonomy term object.

Uses · 8

Used by · 50

Show all 50

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.

  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.

4.4.0
Converted to return a WP_Term object if $output is OBJECT.
The $taxonomy parameter was made optional.from the docblock
2.3.0
Introduced.from the docblock

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