When registering a new taxonomy, the second argument of register_taxonomy($taxonomy, $object_type, $args)
is the post type (or types) that this taxonomy is registered for.
However, when registering a custom post type, you can also pass taxonomy as one of the arguments:
taxonomies
(array) (optional) An array of registered taxonomies like category or post_tag that will be used with this post type. This can be use in lieu of calling register_taxonomy_for_object_type() directly. Custom taxonomies still need to be registered with register_taxonomy().
Default: None
This is redundant. Which should be used? Can you register a taxonomy without associating it with a specific post type, and then later, when registering the custom post type, associate it then?
As a little backgrounder to why I should care (other than that I don’t like inconsistencies and redundancy): I’m creating a fairly generic taxonomy that I can foresee using across a variety of implementations. I’d love to define this taxonomy once (in a separate file), without any foreknowledge of the custom post types that may want to take advantage of it in the future. Then, on a specific implementation, as I’m creating the custom post type, I know I have this taxonomy that I can associate with the post type. Hope this explains my reasons a bit.
So I dug into the source and discovered that even though the documentation STATES that
$object_type
is “required”, there’s nothing in the logic that makes this so.$object_type
is just added to the properties of the tax object, which presumably is also done when you register the tax while creating the custom post, and also when you useregister_taxonomy_for_object_type()
. I tried creating a custom taxonomy with null for the$object_type
parameter, and then assigning the tax when creating the custom post type and it worked as hoped-for.Codex updated.