最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can multiple custom post types share a custom taxonomy?

programmeradmin0浏览0评论

I'm considering rebuilding a drupal site in wordpress and am still wrapping my head around the differences between the systems - particularly cck/fields and custom content types and the different ways to use taxonomy.

I would like to know if two custom content types can share one custom taxonomy. In drupal I can limit the posting of a particular content type to a group of users and then that posting can have taxonomy that is only shared with one or more other content types (but not all types).

Leaving the user aspect out which appears possible with role scoper, can you do this with wordpress? I've only seen custom content type with custom taxonomy but no way to share a given taxonomy between 2 or more custom content types without recreating it in two places or enabling it globally through categories/tags...

Thanks, -Chad.

I'm considering rebuilding a drupal site in wordpress and am still wrapping my head around the differences between the systems - particularly cck/fields and custom content types and the different ways to use taxonomy.

I would like to know if two custom content types can share one custom taxonomy. In drupal I can limit the posting of a particular content type to a group of users and then that posting can have taxonomy that is only shared with one or more other content types (but not all types).

Leaving the user aspect out which appears possible with role scoper, can you do this with wordpress? I've only seen custom content type with custom taxonomy but no way to share a given taxonomy between 2 or more custom content types without recreating it in two places or enabling it globally through categories/tags...

Thanks, -Chad.

Share Improve this question edited Mar 26, 2013 at 14:47 kaiser 50.9k27 gold badges150 silver badges245 bronze badges asked Mar 26, 2013 at 14:21 ChadDChadD 4031 gold badge4 silver badges6 bronze badges 1
  • Related: wordpress.stackexchange/questions/251877/… – Jesse Nickles Commented Feb 25, 2023 at 22:08
Add a comment  | 

3 Answers 3

Reset to default 20

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.

I was able to achieve this easily by passing array of all the Custom post types I want to share the taxonomy, So my code looked like this:

add_action( 'init', 'build_taxonomies', 0 );
 function build_taxonomies() {
    register_taxonomy( 'some_custom_tax', array('some_post_type_1','some_post_type_2'), array( 'hierarchical' => true, 'label' => 'Custom Tax Title', 'query_var' => true, 'rewrite' => true ) );   
}

From the Codex:

taxonomies

(array) (optional) An array of registered taxonomies like category or post_tag that will be used with this post type. This can be used in lieu of calling register_taxonomy_for_object_type() directly. Custom taxonomies still need to be registered with register_taxonomy().

When you register your post type you assign the taxonomies it supports, or use register_taxonomy_for_object_type() at some other point to add the taxonomy to the post type.

You can assign a taxonomy to as many post types as you like. Taxonomies are not tied to a particular post type.

发布评论

评论列表(0)

  1. 暂无评论