get_object_taxonomies() WordPress Function

The get_object_taxonomies() function retrieves the taxonomies for the specified object. This function is useful for retrieving the taxonomies for a specific object type, such as a post type or a taxonomy. It can also be used to check if a taxonomy is registered for an object type.

get_object_taxonomies( string|string[]|WP_Post $object, string $output = 'names' ) #

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' )

Top ↑

Parameters

$object

(string|string[]|WP_Post)(Required)Name of the type of taxonomy object, or an object (row from posts)

$output

(string)(Optional) The type of output to return in the array. Accepts either 'names' or 'objects'.

Default value: 'names'


Top ↑

Return

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


Top ↑

Source

File: wp-includes/taxonomy.php

function get_object_taxonomies( $object, $output = 'names' ) {
	global $wp_taxonomies;

	if ( is_object( $object ) ) {
		if ( 'attachment' === $object->post_type ) {
			return get_attachment_taxonomies( $object, $output );
		}
		$object = $object->post_type;
	}

	$object = (array) $object;

	$taxonomies = array();
	foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {
		if ( array_intersect( $object, (array) $tax_obj->object_type ) ) {
			if ( 'names' === $output ) {
				$taxonomies[] = $tax_name;
			} else {
				$taxonomies[ $tax_name ] = $tax_obj;
			}
		}
	}

	return $taxonomies;
}


Top ↑

Changelog

Changelog
VersionDescription
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More