Using WordPress ‘get_post_taxonomies()’ PHP function

The get_post_taxonomies() WordPress PHP function retrieves all taxonomy names for the given post.

Usage

get_post_taxonomies( $post );

Input: $post – Post ID or WP_Post object (default is global $post)

Output: An array of taxonomy names for the given post.

Parameters

  • $post (int|WP_Post) – Optional. Post ID or WP_Post object. Default is global $post.

More information

See WordPress Developer Resources: get_post_taxonomies()

Examples

Display all taxonomies of a post

This code retrieves and displays all taxonomies of a post.

$taxonomy_names = get_post_taxonomies();
print_r( $taxonomy_names );

Output:

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

Get taxonomies for a specific post by ID

This example retrieves taxonomies for a post with ID 42.

$post_id = 42;
$taxonomy_names = get_post_taxonomies( $post_id );
print_r( $taxonomy_names );

Get taxonomies for a WP_Post object

This example retrieves taxonomies for a WP_Post object.

$post_object = get_post();
$taxonomy_names = get_post_taxonomies( $post_object );
print_r( $taxonomy_names );

Loop through taxonomies and display their names

This example loops through all taxonomies of a post and displays their names.

$taxonomy_names = get_post_taxonomies();
foreach ( $taxonomy_names as $taxonomy_name ) {
    echo $taxonomy_name . '<br>';
}

Check if a specific taxonomy exists in a post

This example checks if the ‘category’ taxonomy exists in a post’s taxonomies.

$taxonomy_names = get_post_taxonomies();
if ( in_array( 'category', $taxonomy_names ) ) {
    echo 'The post has a "category" taxonomy.';
} else {
    echo 'The post does not have a "category" taxonomy.';
}