Can multiple custom post types share a custom taxonomy?

Sharing a taxonomy between CPTs

I would like to know if two custom content types can share one custom taxonomy.

Simple said: Yes, they can.

How to share

You should always register custom taxonomies and post types to each other as early as possible.

Wrap your registration process in a function, hooked to the init hook at the default priority.

<?php
/** Plugin Name: Register $CPT and $CT */
add_action('init', function() {
    register_taxonomy(
        'some_custom_tax',
        'some_post_type',
        $array_of_arguments
    );
    register_post_type(
        'some_post_type',
        [
            'taxonomies' => [ 'some_custom_tax' ],
            // other arguments
        ]
    );
}, 10 ); # <-- default priority

It doesn’t matter if you use the 2nd argument for register_taxonomy() or if you use register_taxonomy_for_object_type(), as both do the same: They take the $GLOBALS['wp_taxonomies'] array and assign it the post type object (type).

Important note

Just make sure that you register the CT and the CPT to each other at when registering them. Else you won’t have access to that interconnection during query hooks.

Leave a Comment