Support

Account

Home Forums Backend Issues (wp-admin) How do I reserve categories for a Custom Post Type?

Solved

How do I reserve categories for a Custom Post Type?

  • I want to be able to add categories to a Custom Post Type, and have those categories reserved for the CPT and not be used by standard posts, and only appear under the Field Type Taxonomy.

    I’m using the functions below to generate the CPT Report and add a taxonony of categories. With it, I can add categories under the left-hand admin menu, and the standard WordPress metabox appears, too.

    But when I use the ACF field type Taxonomy to select categories while creating a Report, I see all categories, even those for blog posts.

    How do I limit the Field Type to the specific categories for the Report CPT?

    And, how do I get rid of the metabox for categories but retain the admin menu item under the CPT? I tried ‘show_ui’ => false but that kills the admin menu item.

    function report_post_type() {
    
        register_post_type('report',
            array(
                'labels' => array(
                    'name' => __('Reports'),
                    'singular_name' => __('Report'),
                    'add_new' => __('Add New'),
                    'add_new_item' => __('Add Report'),
                    'edit' => __('Edit'),
                    'edit_item' => __('Edit Report'),
                    'new_item' => __('New Report'),
                    'view' => __('View Report'),
                    'view_item' => __('View Report'),
                    'search_items' => __('Search Reports'),
                    'not_found' => __('No reports found'),
                    'not_found_in_trash' => __('No reports found in Trash')
                ),
                'public' => true,
                'hierarchical' => true,
                'has_archive' => false,
                'supports' => array(
                    'title',
                    'revisions'
                ),
                'can_export' => true,
                'menu_position' => 5,
                'menu_icon' => 'dashicons-clipboard',
                'rewrite' => array(
                    'slug' => 'report',
                    'with_front' => false,
                    'hierarchical' => true
                ),
            )
        );
    }
    
    function reports_taxonomy() {
    
        $labels = array(
            'name'                       => _x( 'Report', 'Taxonomy General Name', 'text_domain' ),
            'singular_name'              => _x( 'Report', 'Taxonomy Singular Name', 'text_domain' ),
            'menu_name'                  => __( 'Categories', 'text_domain' ),
            'all_items'                  => __( 'All Items', 'text_domain' ),
            'parent_item'                => __( 'Parent Item', 'text_domain' ),
            'parent_item_colon'          => __( 'Parent Item:', 'text_domain' ),
            'new_item_name'              => __( 'New Item Name', 'text_domain' ),
            'add_new_item'               => __( 'Add New Item', 'text_domain' ),
            'edit_item'                  => __( 'Edit Item', 'text_domain' ),
            'update_item'                => __( 'Update Item', 'text_domain' ),
            'view_item'                  => __( 'View Item', 'text_domain' ),
            'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
            'add_or_remove_items'        => __( 'Add or remove items', 'text_domain' ),
            'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
            'popular_items'              => __( 'Popular Items', 'text_domain' ),
            'search_items'               => __( 'Search Items', 'text_domain' ),
            'not_found'                  => __( 'Not Found', 'text_domain' ),
            'no_terms'                   => __( 'No items', 'text_domain' ),
            'items_list'                 => __( 'Items list', 'text_domain' ),
            'items_list_navigation'      => __( 'Items list navigation', 'text_domain' ),
        );
        $args = array(
            'labels'                     => $labels,
            'hierarchical'               => true,
            'public'                     => true,
            'show_ui'                    => true,
            'show_admin_column'          => true,
            'show_in_nav_menus'          => true,
            'show_tagcloud'              => true,
        );
        register_taxonomy( 'Report', array( 'report' ), $args );
    }
    
    add_action('init', 'report_post_type');
    
    add_action( 'init', 'reports_taxonomy', 2 );
  • To hide the standard WP meta box add this setting to the taxonomy, https://codex.wordpress.org/Function_Reference/register_taxonomy

    
    'meta_box_cb' => false,
    

    When setting up a taxonomy field there is a setting to choose a taxonomy you are using.

    If you are using the built in Category taxonomy then there isn’t any way to limit the terms to just those used on your CPT. And there isn’t any way to prevent people from selecting these categories for post. If you need terms that can only be used for your CPT then you need to create a new taxonomy for this. That’s just the way WP works.

  • Thanks, that’s interesting. I thought I was generating a new taxonomy with the function above. I looked around and I ended up finding https://wordpress.org/plugins/acf-advanced-taxonomy-selector/ which allows me to use specific categories for the CPT and not have them appear anywhere else, and which works with the function above. So that’s what I was looking for.

    And wsing'meta_box_cb' => false does remove the category metabox while leaving the menu item in admin, and works with the plugin and function above.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.