johnbillion / extended-cpts

A library which provides extended functionality to WordPress custom post types and taxonomies.
GNU General Public License v2.0
979 stars 96 forks source link

Adding same taxonomy to multiple post types #80

Closed kaisermann closed 7 years ago

kaisermann commented 7 years ago

Is there a way to create a taxonomy with, let's say register_extended_taxonomy and use its return value as the parameter for $post_type->add_taxonomy()?

Thanks for the awesome lib!

johnbillion commented 7 years ago

register_extended_taxonomy() allows you to pass an array of post types:

register_extended_taxonomy( 'foo', [ 'post', 'page', 'my_cpt' ] );

Also, the $post_type->add_taxonomy() method can be used to add an existing taxonomy to the post type (it first checks whether the taxonomy exists before trying to create it), so you can do something like this:

register_extended_taxonomy( 'foo', 'post' );
$hello_post_type = register_extended_post_type( 'hello' );
$hello_post_type->add_taxonomy( 'foo' );

Does that answer your question? As long as you register the post type(s) before the taxonomy you'll usually be able to do what you want to do.

kaisermann commented 7 years ago

Yeah it does! Thank you very much 😄