Based on your code and capabilities setup, the issue occurs because the built-in Categories and Tags taxonomies require the primitive edit_posts capability for term assignment, but your Writer role only has custom capabilities like edit_stories. Here’s how to fix it:
Solution: Map Taxonomy Capabilities to Your CPT
Add this code to your theme’s functions.php or a custom plugin:
function fix_story_taxonomy_caps() {
global $wp_taxonomies;
// Update capabilities for Categories
if (isset($wp_taxonomies['category'])) {
$wp_taxonomies['category']->cap->assign_terms="edit_stories"; // Use your CPT capability
}
// Update capabilities for Tags
if (isset($wp_taxonomies['post_tag'])) {
$wp_taxonomies['post_tag']->cap->assign_terms="edit_stories"; // Use your CPT capability
}
}
add_action('init', 'fix_story_taxonomy_caps', 20); // Run after CPT registration
Explanation:
Why it fails?
- Built-in taxonomies (category/post_tag) default to edit_posts for
assignment - Your Writer role lacks edit_posts (only has edit_stories)
- WordPress doesn’t automatically map taxonomy capabilities to CPTs
What the fix does?
- Changes the required capability for assigning terms to edit_stories
- Your Writer role has this capability (edit_stories => 1)
- Maintains security – writers can assign terms but not manage them
Additional Recommendations:
Grant manage_categories if needed:
// Add to your role setup code
$writer = get_role('writer');
$writer->add_cap('manage_categories'); // If writers should create/edit terms
Alternative: Custom Taxonomies
For better control, register custom taxonomies instead of using built-in ones:
register_taxonomy('story_category', 'story', [
'capabilities' => [
'assign_terms' => 'edit_stories', // Match CPT capability
// Other capabilities...
]
]);
Verification Steps:
- Log in as a Writer
- Create/edit a Story
- Categories/Tags meta boxes should now appear
- Verify assignments are saved correctly
This solution maintains your security structure while enabling taxonomy assignment for your custom post type.