Being given a taxonomy or term, is is possible to pull any / all post types the taxonomy is attached to?

For example, knowing the built in taxonomy category or being given a term in the category taxonomy, is there a function to reverse engineer that category is a taxonomy of post ?

Unneeded Info

I’m working with some custom fields in taxonomy terms that specifically need to know the post type, but if the post type parameter in the URL isn’t there for whatever reason I run into trouble.

3

If we peek into the global $wp_taxonomies variable we see the associated object types.

There might be better ways to do this or even core functions, but you could try the following:

function wpse_172645_get_post_types_by_taxonomy( $tax = 'category' )
{
    global $wp_taxonomies;
    return ( isset( $wp_taxonomies[$tax] ) ) ? $wp_taxonomies[$tax]->object_type : array();
}

then for the default setup you get:

$out = wpse_172645_get_post_types_by_taxonomy( 'category' );
print_r( $out );

with the output:

Array
(
    [0] => post
)

Leave a Reply

Your email address will not be published. Required fields are marked *