0

I am running into an issue that I am trying to fix. I first needed to find a way to add a category page to custom post type. Which I was able to do with this following code added to the mu-plugin file.

'taxonomies' => array('contractors', 'category'),

However, the problem is that when I select the category page for the custom post type "contractors", I also get all of the categories for the blog post in the same page. I only want the page in the admin panel to display categories for different types of contractors. Not for all of the post types in the website.

When categories in contractor post type is selected. I get this following URL destination in the admin panel. However, it also comes with all categories previously created for blog post. Need to omit those from this category.

/wp-admin/edit-tags.php?taxonomy=category&post_type=contractors

How can I get rid of all of the categories not associated with this custom post type?

Thank you

4
  • Why not just use a separate taxonomy? The only reason you’d want to use category was if you did want to share them. Mar 14, 2023 at 4:43
  • OK, so maybe I understood this incorrectly. I wanted to create the taxonomy for categories of "contractors", not category itself. But I wanted to do this in the mu-plugins folder. I just thought that it was making an association between a category type called "contractors". I don't want the category type to be category. I wanted to have the category page in the admin panel to only display category options for contractors such as "Painters", "Fencing Companies", "Roofers". Right now, it shows all categories in blog post. They shouldn't be in this admin category page. Thank you for the reply. Mar 14, 2023 at 14:54
  • Yes, what I'm saying is that you can't have separate categories for Contractors and blog posts. What you need to do is create a new taxonomy called "Contractor category", or something similar, and assign those to your contractors. Mar 14, 2023 at 14:59
  • ok, understood. Thank you for your help. Mar 14, 2023 at 15:06

1 Answer 1

0

Thanks to @Jacob Peattie for pointing me in the right direction. I was able to get the code to display exactly what I needed it to with the following syntax. My intent was to get an admin level category page to only display new categories for contractors like painters, fencing, roofers, etc. Initially, it redirected me to the blog post categories which I didn't want to use. Now with the following register_taxonomy function, I was able to get a new admin page for the contractor's categories.

    // Contractor Post Types
register_post_type('contractors', array(
    'show_in_rest' => true,
    'supports' => array('title', 'editor', 'excerpt', 'thumbnail','taxonomies'),
    'rewrite' => array('slug' => 'contractors'),
    'has_archive' => true,
    'public' => true,
    'labels' => array(
        'name' => 'Contractors',
        'add_new_item' => 'Add New Contractor',
        'edit_item' => 'Edit_Contractor',
        'all_items' => 'All Contractors',
        'singular_name' => 'Contractor'
    ),
    'menu_icon' => 'dashicons-admin-tools',
    'taxonomies' => array
    (
        'contractors_category',
        'post_tag'
    )
));

// Registering the contractor taxonomy
   register_taxonomy('contractors_category', 'contractors', array(
        'label' => 'Categories',
        'rewrite' => array('slug' => 'contractors-category'),
        'show_in_rest' => true,
        'hierarchical' => true
   ));

    // Registering the contractor tags taxonomy
   register_taxonomy('contractors_tags', 'contractors', array(
        'label' => 'Tags',
        'rewrite' => array('slug' => 'contractors-tags'),
        'show_in_rest' => true
   ));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.