0
votes

According to the WordPress Codex, the get_post_types() function has a 'taxonomies' parameter, but there is no explanation of exactly how it works.

$post_args = array(
            'public'   => true,
            'taxonomies' => 'genre',
            'show_ui' => true
            ); 

$post_types = get_post_types($post_args);

This above code doesn't return anything even though genre is a taxonomy that is registered to the 'book' post type.

If i have a custom post type of 'books' with some custom taxonomies of say, 'author' and 'genre'... I'd like to use get_post_type() to return the post type associated with 'genre'... obviously 'books'. I'm trying to make something that will help set the post_type parameter in get_posts().. since that only searches posts by default and not custom post types. Obviously I could just set the post_type parameter, but I want to make it forward compatible with any future post types I might use in my different themes.

2

2 Answers

7
votes

Although I'm a bit late to the party on this one:

taxonomies isn't a valid argument for get_post_types(), so at best it will simply ignore the argument and return a list of all registered post types.

Probably your best bet is the get_taxonomy() object:

$currentTaxonomy = get_query_var('taxonomy');

//  Or: $currentTaxonomy = 'genre';

if ($currentTaxonomy) {
    $taxObject = get_taxonomy($currentTaxonomy);
    $postTypeArray = $taxObject->object_type;
}

This will give you an array of all post types that taxonomy is registered to.

0
votes

Might want to use a combination of both: get_posts() and then pass in your post_type. Something like below might help get you started.

$post_types = get_post_types();
    if ( is_category() || is_tag()) {

        $post_type = get_query_var('article');

        if ( $post_type )
            $post_type = $post_type;
        else
            $post_type = $post_types;

        $query->set('post_type', $post_type);

    return $query;
    }
}

You would NOT want to use is_category and is_tag, you would use something like is_in_taxonomy(). What exactly are you trying to do? I think you're trying to do the same thing as me which is...

on archive page for custom post type use a custom loop for ALL taxonomies of certain custom post type. like the following:

taxonomy-[MY-CPT].php

vs.

taxonomy-[MY-CUSTOM-TAXONOMY].php

I'm actually trying to do the same as we speak, I'll let you know what I come up with.

NOTE there I just made up the is_in_taxonomy()