get_object_taxonomies()WP 2.3.0

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

Example:

$taxonomies = get_object_taxonomies( 'post' );

This results in:

Array( 'category', 'post_tag' )
1 time — 0.0000272 sec (very fast) | 50000 times — 0.19 sec (very fast) | PHP 7.4.25, WP 6.0.1

No Hooks.

Return

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

Usage

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

Examples

0

#1 Taxonomy names for the post type

$taxonomy_names = get_object_taxonomies( 'post' );
print_r( $taxonomy_names );

/*
will take it out:

Array
(
	[0] => category
	[1] => post_tag
	[2] => post_format
)
*/
0

#2 Taxonomy objects for the post type

A similar example to the previous one that returns taxonomies related to the post type as data objects for each taxonomy:

$taxonomy_objects = get_object_taxonomies( 'post', 'objects' );

print_r( $taxonomy_objects );

/*
will take it out:

Array
(
	[category] => stdClass Object
		(
			[hierarchical] => 1
			[update_count_callback] =>
			[rewrite] =>
			[query_var] => category_name
			[public] => 1
			[show_ui] => 1
			[show_tagcloud] => 1
			[_builtin] => 1
			[labels] => stdClass Object
				(
					...
				)

			...

			[name] => category
			[label] => Categories
		)

	[post_tag] => stdClass Object
		(
			...
		)

	[post_format] => stdClass Object
		(
			....
		)

)
*/
0

#3 Taxonomy names for the post object

To get the names of taxonomies supported by the current post, you need to pass the entire post object instead of the post type name:

add_action( 'wp_head', 'get_current_post_taxonomies' );

function get_current_post_taxonomies(){
	global $post;

	$taxonomy_names = get_object_taxonomies( $post );

	print_r( $taxonomy_names );
}

/*
will take it out:

Array
(
	[0] => category
	[1] => post_tag
	[2] => post_format
)
*/
0

#4 We obtain taxonomies of several post types

Its nice to know that I can get multiple post type taxonomies at once without making a loop.

Suppose we have two types of posts with taxonomies: post (native) and mypost (custom), then:

$tax_names = get_object_taxonomies( [ 'post', 'mypost' ] );

/* $tax_names
Array
(
	[0] => category    // dachshund post
	[1] => post_tag    // post taxa
	[2] => post_format // taxa post
	[3] => mypost_tag  // mypost dachshund
	[4] => mypost_cat  // mypost dachshund
)
*/

Or we can get all data as objects, but not names only:

$tax_names = get_object_taxonomies( [ 'post', 'mypost' ], 'objects' );

Notes

  • Global. WP_Taxonomy[]. $wp_taxonomies The registered taxonomies.

Changelog

Since 2.3.0 Introduced.

get_object_taxonomies() code WP 6.6.2

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;
}