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

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

Parameters

$object_typestring|string[]|WP_Postrequired
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

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

Source

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

VersionDescription
2.3.0Introduced.

User Contributed Notes

  1. Skip to note 5 content

    Taxonomy objects for post type
    If the $output parameter is 'objects', taxonomy objects will be returned as described in get_taxonomies().

    <?php 
    	$taxonomy_objects = get_object_taxonomies( 'post', 'objects' );
    	print_r( $taxonomy_objects);
    ?>

    will output

    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
            (
                ....
            )
    )
  2. Skip to note 7 content

    Taxonomy names for post object
    To get the taxonomies for the current post, the current post object can be passed instead of the post type.

    <?php 
    add_action('wp_head','wpdocs_output_current_post_taxonomies');
    
    /**
     * Output taxonomies for the current post
     */
    function wpdocs_output_current_post_taxonomies(){
    	global $post;
    
    	$taxonomy_names = get_object_taxonomies( $post );
    	print_r( $taxonomy_names );
    }
    ?>

    will output

    Array
    (
        [0] => category
        [1] => post_tag
        [2] => post_format
    )
  3. Skip to note 8 content

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

    <?php 
    $post_taxonomies_objects = get_object_taxonomies( array( 'page', 'post', 'custom_post_type_slug' ), 'objects' );
    
    /**
     * Output taxonomies objects for the selected post types.
     */
    print_r( $post_taxonomies_objects );
    ?>

    or for just a array with taxonomies names

    <?php
    $post_taxonomies_names = get_object_taxonomies( array( 'page', 'post', 'custom_post_type_slug' ), 'names );
    
    /**
     * Output taxonomies names for the selected post types.
     */
    print_r( $post_taxonomies_names );
    ?>

You must log in before being able to contribute a note or feedback.