One Taxonomy Shared with Multiple Custom Post Type

Question

My code below is for WordPress registering multiple custom post type sharing the same one taxonomy. The taxonomy and CPT are registered successfully, however it shows the tax at the second CPT only while the CPT has no taxonomy.

So as the example below expertise_type is shown with Teams but not with Expertise. Another issue I had with the code is has_archive if I make it true the posts of both CPT are the same while if I turns it off the posts work well.

<?php
function example_cpt_array() {
    
    $posts = array(

        'expertise' => array(
            'singular_name'         => 'Experience',
            'general_name'          => 'Expertise',
            'dashicon'              => 'dashicons-clipboard',
        --> 'taxonomy'              => 'expertise_type', //taxonomy name
        --> 'taxonomy_label'        => 'Type',
        --> 'taxonomy_slug'         => 'expertise-type', //taxonomy slug
            'exclude_from_search'   => false,
            'show_in_nav_menus'     => true,
            'query_var'             => true,
            'rewrite'               => array('slug' => 'expertise'),
            'hierarchical'          => false,
            'supports'              => array( 'title', 'editor', 'thumbnail', 'revisions' ),
        ),
    
        'team' => array(
            'singular_name'         => 'Team',
            'general_name'          => 'Teams',
            'dashicon'              => 'dashicons-groups',
        --> 'taxonomy'              => 'expertise_type', //taxonomy name
        --> 'taxonomy_label'        => 'Type',
        --> 'taxonomy_slug'         => 'expertise-type', //taxonomy slug
            'exclude_from_search'   => false,
            'show_in_nav_menus'     => true,
            'query_var'             => true,
            'rewrite'               => array('slug' => 'team'),
            'hierarchical'          => false,
            'register_meta_box_cb'  => 'add_team_metaboxes',
            'supports'              => array( 'title', 'editor', 'thumbnail', 'revisions' ),
        ),
    );
    
    $posts = apply_filters( 'example_cpt_array', $posts );
    
    return $posts;
}
add_action( 'init', 'example_register_cpt' );

/**
* Register post types.
*
*/
function example_register_cpt() {
    
    $myarray = example_cpt_array();
    
    foreach ($myarray as $key => $value) {
        $labels = array(
            'name'                  => _x( $value['general_name'], 'Post Type General Name', '' ),
            'singular_name'         => _x( $value['singular_name'], 'Post Type Singular Name', '' ),
            'menu_name'             => __( $value['general_name'], '' ),
            'name_admin_bar'        => __( $value['singular_name'], '' ),
            'archives'              => __( $value['singular_name'].' Archives', '' ),
            'attributes'            => __( $value['singular_name'].' Attributes', '' ),
            'parent_item_colon'     => __( 'Parent '. $value['singular_name'].':', '' ),
            'all_items'             => __( 'All '. $value['general_name'], '' ),
            'add_new_item'          => __( 'Add New '. $value['singular_name'], '' ),
            'add_new'               => __( 'Add New', '' ),
            'new_item'              => __( 'New '. $value['singular_name'], '' ),
            'edit_item'             => __( 'Edit '. $value['singular_name'], '' ),
            'update_item'           => __( 'Update '. $value['singular_name'], '' ),
            'view_item'             => __( 'View '. $value['singular_name'], '' ),
            'view_items'            => __( 'View '. $value['general_name'], '' ),
            'search_items'          => __( 'Search '. $value['singular_name'], '' ),
            'not_found'             => __( 'Not found', '' ),
            'not_found_in_trash'    => __( 'Not found in Trash', '' ),
            'featured_image'        => __( 'Featured Image', '' ),
            'set_featured_image'    => __( 'Set featured image', '' ),
            'remove_featured_image' => __( 'Remove featured image', '' ),
            'use_featured_image'    => __( 'Use as featured image', '' ),
            'insert_into_item'      => __( 'Insert into '.$value['singular_name'], '' ),
            'uploaded_to_this_item' => __( 'Uploaded to this '.$value['singular_name'], '' ),
            'items_list'            => __( $value['general_name'] .' list', '' ),
            'items_list_navigation' => __( $value['general_name'] .' list navigation', '' ),
            'filter_items_list'     => __( 'Filter '. $value['general_name'] .'list', '' ),
        );

        $args = array(
            'label'                 => __( $value['singular_name'].'', '' ),
            'description'           => __( $value['singular_name'].' Post Type', '' ),
            'labels'                => $labels,
            'supports'              => $value['supports'],
            'hierarchical'          => $value['hierarchical'],
            'public'                => true,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'menu_icon'             => $value['dashicon'],
            'show_in_admin_bar'     => true,
            'show_in_nav_menus'     => $value['show_in_nav_menus'],
            'can_export'            => true,
        //  'has_archive'           => $value['has_archive'],
            'exclude_from_search'   => $value['exclude_from_search'],
            'publicly_queryable'    => true,
            //'capability_type'         => 'post',
        );
        register_post_type( $key, $args );
    }
}

add_action( 'init', 'example_tax_cpt', 0 );

/**
* Register a taxonomy, post_types_categories for the post types.
*
*/
function example_tax_cpt() {
    
    // Add new taxonomy, make it hierarchical
    $myarray = example_cpt_array();//array of custom post types
    foreach ($myarray as $key => $value) {
        if(isset($value['taxonomy'])) {
            $labels = array(
                'name'                  => _x( $value['general_name'].' '. $value['taxonomy_label'], 'taxonomy general name', '' ),
                'singular_name'         => _x( $value['singular_name'].' '. $value['taxonomy_label'], 'taxonomy singular name', '' ),
                'search_items'          => __( 'Search ' . $value['general_name'], '' ),
                'all_items'             => __( 'All ' . $value['general_name'], '' ),
                'parent_item'           => __( 'Parent ' . $value['taxonomy_label'], '' ),
                'parent_item_colon'     => __( 'Parent ' . $value['taxonomy_label'] . ':', '' ),
                'edit_item'             => __( 'Edit ' . $value['general_name'] .' '. $value['taxonomy_label'], '' ),
                'update_item'           => __( 'Update ' . $value['general_name'] .' '. $value['taxonomy_label'], '' ),
                'add_new_item'          => __( 'Add New ' . $value['general_name'] .' '. $value['taxonomy_label'], '' ),
                'new_item_name'         => __( 'New ' . $value['general_name'] . $value['taxonomy_label'], '' ),
                'menu_name'             => __( $value['singular_name'] .' '. $value['taxonomy_label'], '' ),
            );

            $args = array(
                'hierarchical'          => true,
                'labels'                => $labels,
                'show_ui'               => true,
                'show_admin_column'     => true,
                'show_in_nav_menus'     => true,
                'rewrite'               => array( 'slug' => $value['taxonomy_slug'], 'hierarchical' => true ),
            );
            
            register_taxonomy( $value['taxonomy'], array( $key ), $args );
        }
    }
}
0
David Buik 2 months 0 Answers 13 views 0

Leave an answer