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

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.


Description Description

Example:

$taxonomies = get_object_taxonomies( 'post' );

This results in:

Array( 'category', 'post_tag' )

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

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


Top ↑

Source Source

File: wp-includes/taxonomy.php

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

	if ( is_object( $object ) ) {
		if ( $object->post_type === 'attachment' ) {
			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

Changelog
Version Description
2.3.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    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 3 content
    Contributed by Codex

    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
    )
    

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