It is possible to get a list of post types asociated to a taxonomy?

You retrieve it with $wp_taxonomies already. I just wrote it down, so you’ll probably have to try a little to get the following lines of code up and running, but it should give you an idea of how it should work (see it as reference/pseudo code):

EDIT: After a note from Mike Schinkel i updated the code to make it easier to ignore built in taxonomies (see: _builtin). I hope Mike will post the example he sent me here, so his (much easier) sollution can be marked as the final approach…

// equals the following $keys
$wp_taxonomies['category'] == $wp_taxonomies[0];
$wp_taxonomies['post_tag'] == $wp_taxonomies[1];
$wp_taxonomies['nav_menu'] == $wp_taxonomies[2];
$wp_taxonomies['link_category'] == $wp_taxonomies[3];

// after $key #3 you retrieve all different registered taxonomies
$all_tax = count($wp_taxonomies)-4; // gives you the amount of reg. tax.

// Here starts the actual code
$post_types = array(); // some empty array to save your post types for further procesing
$i = 0;
foreach ( $wp_taxonomies as $tax ) {
if ( !$tax->_builtin)
  $post_type_arr = $tax[$i]->object_type; // array of post types in the current taxonomy
  foreach ( $post_type_arr as $arr ) : // loop through all post types assigned to the taxonomy
    $post_types[] .= $arr; // assign them to our array of post types
  endforeach;
  $i++;
}
$post_types = array_unique($post_types); // drop doublettes out of the array
var_dump($post_types);

# expected result close to this...

# Array(
#   1 => $post_type_a,
#   2 => $post_type_b,
#   3 => $post_type_n
# );

Leave a Comment