VickyBroz VickyBroz - 4 months ago 14
PHP Question

Wordpress post types posts mixing in backend non admine roles

I'm building a wordpress theme from scratch. I have some experience but I'm very meticulous with my customization so I'm encountering some problems at this point.

The issue is the following: I've created some special post types, with their own capabilities and allowed some customized new roles to access to them. Everything works but when this new role -'writer'- writes on the back end the posts of the different post types I've created got mixed up in the post type dashboard, right where it lists the posts.

So we have these post types:
- Reviews
- Previews

The reviews dashboard lists both the reviews and the previews posts written by that writer and the same happens to the previews dashboard.

This does not happen to the admin. That is what makes me think it's a permission thing.

Here is the post type code:

// ==================================== //
// R E V I E W S //
// ==================================== //
// Register Custom Post Type

function custom_post_type_reviews() {

$labels = array(
'name' => _x( 'Reviews', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Review', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Reviews', 'text_domain' ),
'name_admin_bar' => __( 'Reviews', 'text_domain' ),
'archives' => __( 'Reviews archive', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All the reviews', 'text_domain' ),
'add_new_item' => __( 'Add new review', 'text_domain' ),
'add_new' => __( 'Add new', 'text_domain' ),
'new_item' => __( 'Add item', 'text_domain' ),
'edit_item' => __( 'Edit item', 'text_domain' ),
'update_item' => __( 'Update item', 'text_domain' ),
'view_item' => __( 'View item', 'text_domain' ),
'search_items' => __( 'Search Item', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in trash', 'text_domain' ),
'featured_image' => __( 'Featured image', 'text_domain' ),
'set_featured_image' => __( 'Add featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use featured image', 'text_domain' ),
'insert_into_item' => __( 'insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Update to this item', 'text_domain' ),
'items_list' => __( 'Items lists', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
);

$capabilities = array(
'publish_posts' => 'publish_reviews',
'edit_posts' => 'edit_reviews',
'edit_others_posts' => 'edit_others_reviews',
'delete_posts' => 'delete_reviews',
'delete_others_posts' => 'delete_others_reviews',
'read_private_posts' => 'read_private_reviews',
'edit_post' => 'edit_review',
'delete_post' => 'delete_review',
'read_post' => 'read_review',
);

$args = array(
'label' => __( 'Reviews', 'text_domain' ),
'description' => __( 'Reviews', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'author','comments' ),
'taxonomies' => array( 'categories', 'post_tag', ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-media-text',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'map_meta_cap' => true,
'capability_type' => 'review',
'capabilities' => $capabilities,
);
register_post_type( 'reviews', $args );

}
add_action( 'init', 'custom_post_type_reviews', 0 );


For creating the roles I've installed a plug in called Memebers I'm attaching an image of the capabilities assigned to the 'role' I've created.
Role capabilities

I have no clue about what this could be, yet I'm not a word press pro.

THANKS!!!

Answer

I see various potential in the combination of map_meta_cap, capability_type and capabilities Have a look in the Codex for all of them. I think it should work in following cases:

(1)

  • capability_type set as review
  • map_meta_cap set to false
  • do not need to use capabilities

the roles in your Role capabilities should be matching plugin and singular to the the Codex - I mean "review" or "reviews".

(2)

  • capability_type set as review
  • map_meta_cap set to true
  • use capabilities - check the Codex

Check if the array inside $capabilites is matching the $capabilities used in the Role editor.

Comments