Using WordPress ‘is_object_in_taxonomy()’ PHP function

The is_object_in_taxonomy() WordPress PHP function determines if the given object type is associated with the given taxonomy.

Usage

is_object_in_taxonomy($object_type, $taxonomy);

Parameters

  • $object_type (string) – The object type (e.g., ‘post’, ‘page’, ‘attachment’) you want to check.
  • $taxonomy (string) – The taxonomy name (e.g., ‘category’, ‘post_tag’, ‘custom_taxonomy’) you want to check.

More information

See WordPress Developer Resources: is_object_in_taxonomy()

Examples

Check if ‘post’ is associated with ‘category’

This example checks if the ‘post’ object type is associated with the ‘category’ taxonomy.

if (is_object_in_taxonomy('post', 'category')) {
    echo '**Post** is associated with **Category**.';
} else {
    echo '**Post** is NOT associated with **Category**.';
}

Check if ‘page’ is associated with ‘post_tag’

This example checks if the ‘page’ object type is associated with the ‘post_tag’ taxonomy.

if (is_object_in_taxonomy('page', 'post_tag')) {
    echo '**Page** is associated with **Post Tag**.';
} else {
    echo '**Page** is NOT associated with **Post Tag**.';
}

Check if ‘attachment’ is associated with a custom taxonomy ‘my_taxonomy’

This example checks if the ‘attachment’ object type is associated with the custom taxonomy ‘my_taxonomy’.

if (is_object_in_taxonomy('attachment', 'my_taxonomy')) {
    echo '**Attachment** is associated with **My Taxonomy**.';
} else {
    echo '**Attachment** is NOT associated with **My Taxonomy**.';
}

Check if a custom post type ‘my_post_type’ is associated with ‘category’

This example checks if the custom post type ‘my_post_type’ is associated with the ‘category’ taxonomy.

if (is_object_in_taxonomy('my_post_type', 'category')) {
    echo '**My Post Type** is associated with **Category**.';
} else {
    echo '**My Post Type** is NOT associated with **Category**.';
}

Check if a custom post type ‘my_post_type’ is associated with a custom taxonomy ‘my_taxonomy’

This example checks if the custom post type ‘my_post_type’ is associated with the custom taxonomy ‘my_taxonomy’.

if (is_object_in_taxonomy('my_post_type', 'my_taxonomy')) {
    echo '**My Post Type** is associated with **My Taxonomy**.';
} else {
    echo '**My Post Type** is NOT associated with **My Taxonomy**.';
}