wppaste
WordPress

get_taxonomy( string $taxonomy ): WP_Taxonomy|false

Since
2.3.0
Source
wp-includes/taxonomy.php:350

Looks up a registered taxonomy by its name and returns the matching WP_Taxonomy object, or false if the name isn't registered. Useful when you already have a taxonomy slug (like 'category' or a custom one) and need its labels, capabilities, or object_type without querying terms. Pair it with taxonomy_exists() if you only need a yes/no check.

Retrieves the taxonomy object of $taxonomy.

Description

The get_taxonomy function will first check that the parameter string given is a taxonomy object and if it is, it will return it.

Compatibility

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

$taxonomystring
Name of taxonomy object to return.

Return value

WP_Taxonomy|false
The taxonomy object or false if $taxonomy doesn't exist.

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.

Display the label and post types for a built-in taxonomy

Categories and tags are already registered on every WordPress install, so this runs against the built-in 'category' taxonomy.

$taxonomy = get_taxonomy( 'category' );

if ( $taxonomy ) {
	printf(
		'Label: %s | Object types: %s',
		esc_html( $taxonomy->label ),
		esc_html( implode( ', ', $taxonomy->object_type ) )
	);
} else {
	echo 'Taxonomy not found.';
}

Check whether a custom taxonomy is registered before reading its capabilities

Register a custom 'genre' taxonomy for posts, then confirm it exists and inspect its manage_terms capability.

$genre_taxonomy = get_taxonomy( 'genre' );

if ( $genre_taxonomy ) {
	printf(
		'Hierarchical: %s | manage_terms cap: %s',
		$genre_taxonomy->hierarchical ? 'yes' : 'no',
		esc_html( $genre_taxonomy->cap->manage_terms )
	);
} else {
	echo 'Genre taxonomy is not registered.';
}

The taxonomy has to be registered on every request via register_taxonomy() before get_taxonomy() can find it.

Common problems and fixes · 3

Why does get_taxonomy() return false for a taxonomy I know I registered?

The function calls taxonomy_exists() internally, which only checks the $wp_taxonomies global. If your register_taxonomy() call runs on a hook that fires after the point where you're calling get_taxonomy(), or it never runs on that request at all, the lookup fails. - Confirm register_taxonomy() runs on 'init' or earlier on every request, not just once in an activation hook. - Double-check the exact taxonomy slug string for typos or a mismatched prefix.

How do I get every registered taxonomy instead of just one?

get_taxonomy() only accepts a single taxonomy name and returns one object (or false). It has no way to loop over or filter multiple taxonomies. - Use get_taxonomies() with an $output argument of 'objects' to get all matching WP_Taxonomy objects at once.

What's on the WP_Taxonomy object get_taxonomy() returns?

The parsed return type is WP_Taxonomy|false, but the function itself just pulls the stored object out of $wp_taxonomies without transforming it, so every property set during registration (label, labels, object_type, cap, hierarchical, rewrite, show_ui, and more) is available on the object you get back.

Alternatives and related functions

get_taxonomies
When you need a list of multiple registered taxonomies (optionally filtered by args) instead of a single one by name.
taxonomy_exists
When all you need is a true/false check and don't need the full WP_Taxonomy object.
register_taxonomy
When the taxonomy doesn't exist yet and you need to create it before get_taxonomy() can find it.
get_taxonomy_labels
When you specifically need the labels object for a taxonomy rather than the whole WP_Taxonomy instance.

Performance profile

How much work a call to get_taxonomy() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
7–8

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

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

What one call costs · 1 distinct outcome

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

WhenInstructionsCalls it makes
always7–8taxonomy_exists()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 9 instructions, 7–8 executed per call, 1 branch. 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.

Uses · 1

Used by · 50

Show all 50

Source code

function get_taxonomy( $taxonomy ) {	global $wp_taxonomies; 	if ( ! taxonomy_exists( $taxonomy ) ) {		return false;	} 	return $wp_taxonomies[ $taxonomy ];}

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.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.