I'm trying to separate the category lists for a specific post type "announcement" from the post categories.
I want my announcement post type to have its own category list and not mix with the post category list
Here's my code
function announcement_post_types() {
register_post_type(
'announcement',
array(
'labels' => array(
'name' => __( 'Announcements' ),
'singular_name' => __( 'announcement' ),
'menu_name' => __( 'Announcements' ),
'all_items' => __( 'All Announcements' ),
'view_item' => __( 'View Announcement' ),
'add_new_item' => __( 'New Announcement' ),
'add_new' => __( 'New Announcement' ),
'edit_item' => __( 'Edit Announcements' ),
'update_item' => __( 'Update' ),
'search_items' => __( 'Search' ),
'not_found' => __( 'Not Found' ),
'not_found_in_trash' => __( 'Not Found in Trash' ),
),
'public' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'announcement'),
'menu_icon' => 'dashicons-admin-page',
'supports' => array('title', 'editor', 'custom-fields')
)
);
register_taxonomy(
'announcement_type',
'announcement', // this is the custom post type(s) I want to use this taxonomy for
array(
'hierarchical' => false,
'label' => 'News Types',
'query_var' => true,
'rewrite' => true
)
);
register_taxonomy_for_object_type('category', 'announcement');
}
add_action('init', 'announcement_post_types');
If this code is working, this is what links the regular categories to the announcement post type:
register_taxonomy_for_object_type('category', 'announcement');
We need to exchange it with the new taxonomy you created and registered, so exchange it with this:
register_taxonomy_for_object_type('announcement_type', 'announcement');
Let me know if this did the trick for you, there might be more issues here, but this might also be enough.
UPDATE:
I removed the register_taxonomy_for_object_type('category', 'announcement');
and changed into this
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' =>_x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'popular_items' => __( 'Popular Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Announcement Category' ),
'update_item' => __( 'Update Announcement Category' ),
'add_new_item' => __( 'Add New Announcement Category' ),
'new_item_name' => __( 'New Announcement Category' ),
'separate_items_with_commas' => __( 'Separate categories with commas' ),
'add_or_remove_items' => __( 'Add or remove Announcement categories' ),
'choose_from_most_used' => __( 'Choose from the most used categories' )
);
register_taxonomy(
'announcement_type',
'announcement', // this is the custom post type(s) I want to use this taxonomy for
array(
'hierarchical' => false,
'label' => 'News Types',
'query_var' => true,
'label' => __('Announcement Category'),
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'rewrite' => true
)
);
It works for me now,
If you have better answer for this please share :) I want to make this code shorter.