On StackOverflow user indextwo suggests to do the following:
Use the get_taxonomy( $taxonomy-slug )
function which returns an object that contains all the associated post types. As suggested in the comments, we can then use get_post_type_object( $post-type-slug )
to get the object and Singular Label.
$post_type_names = array(); // Array( 'post-type-slug' => 'Post Type Label' );
$taxonomy = get_query_var( 'taxonomy' );
if( ! empty( $taxonomy ) ) {
$tax_object = get_taxonomy( $taxonomy );
$cpt_array = $tax_object->object_type;
if( ! empty( $cpt_array ) ) {
$post_type_names = array_flip( $cpt_array );
foreach( $post_type_names as $cpt_slug => $tmp ) {
$post_type_names[ $cpt_slug ] = get_post_type_object( $cpt_slug )->labels->singular_name;
}
}
}
The above will return the post type slugs in a generic Array.
Array(
[post-type-slug] => 'Post Type Name',
[post-type-slug2] => 'Post Type Name 2',
)