It turns out that capabilities for custom post types don’t actually exist unless you use add_cap to the role, for example for the following custom post type:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | add_action( 'init', 'register_cpt_gallery' ); function register_cpt_gallery() { $labels = array( 'name' => __( 'Galleries', 'gallery' ), 'singular_name' => __( 'Gallery', 'gallery' ), 'add_new' => __( 'Add New', 'gallery' ), 'add_new_item' => __( 'Add New Gallery', 'gallery' ), 'edit_item' => __( 'Edit Gallery', 'gallery' ), 'new_item' => __( 'New Gallery', 'gallery' ), 'view_item' => __( 'View Gallery', 'gallery' ), 'search_items' => __( 'Search Galleries', 'gallery' ), 'not_found' => __( 'No galleries found', 'gallery' ), 'not_found_in_trash' => __( 'No galleries found in Trash', 'gallery' ), 'parent_item_colon' => __( 'Parent Gallery:', 'gallery' ), 'menu_name' => __( 'Galleries', 'gallery' ), ); $args = array( 'labels' => $labels, 'hierarchical' => true, 'description' => 'Image galleries for teachers classes', 'supports' => array( 'title', 'editor', 'author'), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_icon' => get_bloginfo('template_url') . '/images/imagegallery.png', 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'query_var' => true, 'can_export' => true, 'rewrite' => true, 'capabilities' => array( 'edit_post' => 'edit_gallery', 'edit_posts' => 'edit_galleries', 'edit_others_posts' => 'edit_other_galleries', 'publish_posts' => 'publish_galleries', 'read_post' => 'read_gallery', 'read_private_posts' => 'read_private_galleries', 'delete_post' => 'delete_gallery' ), // as pointed out by iEmanuele, adding map_meta_cap will map the meta correctly 'map_meta_cap' => true ); register_post_type( 'gallery', $args ); } |
The additional capabilities should be added to a role for the permissions to actually work in the backend:
1 2 3 4 5 6 7 8 9 10 11 12 13 | function add_theme_caps() { // gets the administrator role $admins = get_role( 'administrator' ); $admins->add_cap( 'edit_gallery' ); $admins->add_cap( 'edit_galleries' ); $admins->add_cap( 'edit_other_galleries' ); $admins->add_cap( 'publish_galleries' ); $admins->add_cap( 'read_gallery' ); $admins->add_cap( 'read_private_galleries' ); $admins->add_cap( 'delete_gallery' ); } add_action( 'admin_init', 'add_theme_caps'); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.