Using WordPress ‘get_object_taxonomies()’ PHP function

The get_object_taxonomies() WordPress PHP function 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.

Usage

get_object_taxonomies('post');

Input: ‘post’

Output: Array(‘category’, ‘post_tag’)

Parameters

  • $object_type (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 ‘names’.

More information

See WordPress Developer Resources: get_object_taxonomies()

Examples

Taxonomy objects for post type

This code will return taxonomy objects for the ‘post’ post type.

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

Taxonomy names for post type

This code will return taxonomy names for the ‘post’ post type.

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

Taxonomy names for post object

This code will return taxonomy names for the current post.

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

Get multiple post type taxonomies with objects

This code will return taxonomy objects for the selected post types.

$post_taxonomies_objects = get_object_taxonomies(
    array('page', 'post', 'custom_post_type_slug'),
    'objects'
);
print_r($post_taxonomies_objects);

Get multiple post type taxonomies with names

This code will return taxonomy names for the selected post types.

$post_taxonomies_names = get_object_taxonomies(
    array('page', 'post', 'custom_post_type_slug'),
    'names'
);
print_r($post_taxonomies_names);