wppaste
WordPress

get_object_taxonomies( string|string[]|WP_Post $object_type, string $output = 'names' ): string[]|WP_Taxonomy[]

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

Lists the taxonomies attached to a post type, an array of post types, or a post object, returned as taxonomy names or as full WP_Taxonomy objects. It reads from the global $wp_taxonomies registry, so it only sees taxonomies that have already been registered by the time it runs. Passing an attachment object routes the call to get_attachment_taxonomies() instead of doing the normal object_type match.

Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name.

Description

Example:

$taxonomies = get_object_taxonomies( 'post' );

This results in:

Array( 'category', 'post_tag' )

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

$object_typestring|string[]|WP_Post
Name of the type of taxonomy object, or an object (row from posts).
$outputstringoptional
The type of output to return in the array. Accepts either 'names' or 'objects'. Default 'names'.Default: 'names'

Return value

string[]|WP_Taxonomy[]
The names or objects of all taxonomies of $object_type.

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 the taxonomies registered for the post post type

Print the taxonomy names that WordPress ties to standard posts.

$taxonomies = get_object_taxonomies( 'post' );

echo '<pre>' . esc_html( print_r( $taxonomies, true ) ) . '</pre>';

Core registers category and post_tag on the post post type, so both names show up by default.

Get taxonomy objects for a custom post type instead of just names

Register a book post type with its own genre taxonomy, then pull back full WP_Taxonomy objects for it.

$taxonomies = get_object_taxonomies( 'book', 'objects' );

foreach ( $taxonomies as $taxonomy ) {
	echo esc_html( $taxonomy->label ) . '<br>';
}

Anything other than the literal string 'names' for $output returns objects, so a typo like 'object' still works.

Common problems and fixes · 4

Why does get_object_taxonomies() return an empty array for my custom post type?

The function only matches taxonomy object_type arrays it finds in the global $wp_taxonomies. If your call runs before register_taxonomy() has executed for that post type on the current request, or before register_post_type() has run, there is nothing yet to match against.

Why did passing $post_type as a WP_Post object give me totally different results?

The source checks is_object( $object_type ) first, and if the object's post_type is 'attachment' it hands the whole call off to get_attachment_taxonomies(), which matches by mime type rather than by the object_type strings other taxonomies use.

Why does $output = 'object' (singular) still give me objects instead of an error?

The function does not validate $output against a whitelist. It only special-cases the exact string 'names'; anything else falls through to the else branch and returns WP_Taxonomy objects keyed by taxonomy name.

Can I pass an array of post types to get taxonomies shared across several types?

Yes. The source casts $object_type to an array with (array) $object_type and then uses array_intersect() against each taxonomy's object_type list, so a taxonomy is included if it applies to any of the types you pass in.

Alternatives and related functions

get_taxonomies
When you need every registered taxonomy in the site regardless of which post types or objects it is attached to.
get_attachment_taxonomies
When you are specifically working with attachments, since get_object_taxonomies() delegates to this function internally for that case.
is_object_in_taxonomy
When you only need a yes or no answer for whether one specific object type is registered in one specific taxonomy.
wp_get_object_terms
When you need the actual term assignments on an object, not just the names of the taxonomies it belongs to.

Performance profile

How much work a call to get_object_taxonomies() 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_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
12–18

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
38

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

What it touches

  • querycontent queryget_post()one call below get_object_taxonomies()

Further down the call graph this can also reach hook, cache, serialize, option 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 · 2 distinct outcomes

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

WhenInstructionsCalls it makes
always12–18none
is_object($object_type)13get_attachment_taxonomies()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 39 instructions, 12–18 executed per call, 6 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.

Uses · 1

Used by · 38

Show all 38

Source code

function get_object_taxonomies( $object_type, $output = 'names' ) {	global $wp_taxonomies; 	if ( is_object( $object_type ) ) {		if ( 'attachment' === $object_type->post_type ) {			return get_attachment_taxonomies( $object_type, $output );		}		$object_type = $object_type->post_type;	} 	$object_type = (array) $object_type; 	$taxonomies = array();	foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {		if ( array_intersect( $object_type, (array) $tax_obj->object_type ) ) {			if ( 'names' === $output ) {				$taxonomies[] = $tax_name;			} else {				$taxonomies[ $tax_name ] = $tax_obj;			}		}	} 	return $taxonomies;}

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