[wp-hackers] $wp_taxonomies is pretty strange - change appreciated

Andrew Nacin wp at andrewnacin.com
Thu Feb 10 22:11:59 UTC 2011


On Thu, Feb 10, 2011 at 4:58 PM, 24/7 <24-7 at gmx.net> wrote:

> ehm... have to write another mail: there's no way around
> $wp_taxonomies. Even if it's internal. get_taxonomies() gives you the
> taxonomies and allows to differ between builtin and custom types, but
> is missing the post types. The original question was "It is possible
> to get a list of post types asociated to a taxonomy?". The guy who
> asked wanted all unique custom post type names that are assigned to
> custom post types, so the original answer on wp stackechange was right
> and there's no solution without $wp_taxonomies.


You don't need to ever use $wp_taxonomies, because get_taxonomies() and
get_taxonomy() are API-level functions that expose its information.

Two examples below. They are entirely untested, and there may even be
another API function I'm not thinking about. (For example, if you wanted to
know all taxonomies tied to a post type, then use get_object_taxonomies()).

$tax_object = get_taxonomy( 'my_taxonomy' );
$post_types = $tax_object->post_type;

Or:

$tax_object = get_taxonomies( array( '_builtin' => false ), 'objects' );
$post_types = array();
foreach ( $tax_object as $tax ) {
   $post_types = array_merge( $post_types, $tax->post_type );
}
$post_types = array_unique( $post_types );


More information about the wp-hackers mailing list