• Resolved cogmios

    (@cogmios)


    I just wondered if I can use register_taxonomy to re-use certain taxonomies over multiple custom post types (i can via the taxonomies) if the properties defined are different from one custom post type to another.

    E.g. hierarchical with one and non hierarchical with the other.

    E.g. on https://codex.www.ads-software.com/Function_Reference/register_taxonomy

    it says for the second parameter:

    {{Parameter|$object_type|array/string|A name or array of names of the registered [[Glossary#Post_Type|post type(s)] for this taxonomy object.}}

    probably not and I should name it “taxonomy1” and “taxonomy2”

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter cogmios

    (@cogmios)

    so what it boils down to:

    can i first register a set of taxonomies (without using the second parameter)

    and only then later bind them to a custom post (possible with custom properties)

    OR should I first register it with at least 1 custom post type and later add more via : register_taxonomy_for_object_type

    (in that case i would probably make a “dummy custom post type” for initial registering all taxonomies)

    I wonder what the best route here is too. I just registered some taxonomies and left the object blank because I thought I might want to add it to multiple places in the future. However, when I do leave it blank and enable the taxonomy within register_post_type the menu items in the dashboard for the custom taxonomy edit pages are all titled “Post Tags”. It’s only when I add an object that the edit pages are marked with the taxonomy name correctly.

    If you want to add taxonomy support to a post type the correct method (i believe) would be to use register_taxonomy_for_object_type, which does just that, registers taxonomy support for the supplied type or types of post.

    Thanks t310s_,

    Turns out my initial question was slightly different to cogmios’, but I think I’ve found the answers to both anyway.

    register_taxonomy_for_object_type doesn’t seem to do anything; at least it didn’t in my tests. Unless a taxonomy is assigned to an object from the second parameter of register_taxonomy or from register_post_type it doesn’t show up in the dashboard.

    I didn’t realise before that the second parameter of register_taxonomy can accept a single object type string, an array, or a null value. As far as I can tell, this parameter is the only way to assign a taxonomy to an existing object (such as posts and pages), but you can also assign a taxonomy to a custom post type from within register_post_type, and when you do so, it doesn’t matter what objects are in register_taxonomy.

    So, in summary, to assign a custom taxonomy to multiple custom post types, either use an array in the second parameter of register_taxonomy(e.g., register_taxonomy('example',array('post','page','custom-post-type');) and add your new custom post types as you go along, or leave that parameter blank (e.g., register_taxonomy('example','');) and add the taxonomies when registering the new post types.

    Thank you Kristarella, your explanation was exactly what I was looking for. I now have the right taxonomies on the right content types ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘register_taxonomy to re-use taxononmies over multiple custom post types?’ is closed to new replies.