How to fix post_tags not showing for a CPT in new post for a custom user role?

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?

  1. Built-in taxonomies (category/post_tag) default to edit_posts for
    assignment
  2. Your Writer role lacks edit_posts (only has edit_stories)
  3. WordPress doesn’t automatically map taxonomy capabilities to CPTs

What the fix does?

  1. Changes the required capability for assigning terms to edit_stories
  2. Your Writer role has this capability (edit_stories => 1)
  3. 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:

  1. Log in as a Writer
  2. Create/edit a Story
  3. Categories/Tags meta boxes should now appear
  4. Verify assignments are saved correctly

This solution maintains your security structure while enabling taxonomy assignment for your custom post type.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)