Search code examples
phpwordpresscustom-post-typeuser-roles

Why is my custom post type only showing for my custom user role?


I have created a custom post type for my restaurants. It currently is only showing for my custom user role of restaurant_owner. I would like it to also show for the administrator.

What am i doing wrong?

Custom Post Type:

add_action( 'init', 'pt_restaurant');
function pt_restaurant() { 
register_post_type( 'bounty_product', array(
  'labels' => array(
     'name' => 'Restaurants',
     'singular_name' => 'Restaurant',
     'add_new' => 'Add New',
     'add_new_item' => 'Add New Restaurant',
     'edit' => 'Edit',
     'edit_item' => 'Edit Restaurant',
     'new_item' => 'New Restaurant',
     'view' => 'View',
     'view_item' => 'View Restaurant',
     'search_items' => 'Search Restaurants',
     'not_found' => 'No Restaurants found',
     'not_found_in_trash' => 'No Restaurants found in Trash',
     'parent' => 'Parent Restaurant'),
  'description' => 'Used for the Restaurant section',
  'public' => true,
  'capability_type' => array('bounty_product','bounty_products'),
  'map_meta_cap' => true,
  'show_in_menu' => true,
  'menu_position' => 20,
  'has_archive' => true,
  'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),

));

Custom User Role:

add_action('init', 'restaurant_owner_user_role');
function restaurant_owner_user_role() {
    add_role('restaurant_owner', 'Restaurant Owner',
        array (
                'edit_bounty_product' => true,
                'delete_bounty_product' => false,
                'read_bounty_product' => true,
                'publish_bounty_products' => false,
                'edit_bounty_products' => true,
                'edit_others_bounty_products' => false,
                'delete_bounty_products' => false,
                'delete_others_bounty_products' => false,
                'read_private_bounty_products' => false,            

                'read' => true,
        )
    );
}

Solution

  • you are only giving capabilities to your custom user role that's why admin will not be able to edit/update/delete to your CPT. Try to use following code in functions.php

    function add_theme_caps() {
        // gets the administrator role
        $admins = get_role( 'administrator' );
    
        $admins->add_cap( 'edit_bounty_product' ); 
        $admins->add_cap( 'delete_bounty_product' ); 
        $admins->add_cap( 'read_bounty_product' ); 
        $admins->add_cap( 'publish_bounty_products' ); 
        $admins->add_cap( 'edit_bounty_products' ); 
        $admins->add_cap( 'edit_others_bounty_products' ); 
        $admins->add_cap( 'delete_bounty_products' ); 
        $admins->add_cap( 'delete_others_bounty_products' ); 
        $admins->add_cap( 'read_private_bounty_products' ); 
        $admins->add_cap( 'delete_gallery' ); 
    }
    add_action( 'init', 'add_theme_caps');