I’m not very clear how to restrict admin area post types access to specific user roles.
In my case, I have some post type such as “suppliers” which I do not want to show to “authors”.
By default WP allows authors to browse, add or edit their own content. But I don’t want my authors to add a “supplier” or browse entries from other admins.
I’ve looked into WordPress code and the codex: http://codex.wordpress.org/Function_Reference/register_post_type
there seems to be a parameter in register_post_type( 'supplier', $args )
, which I tried to define in $args as follows, among of course other variables which I don’t think I need to list here since my post type is working fine:
'capabilities' => array( 'edit_others_posts' ),
'map_meta_cap' => true,
I haven’t fully understood the parameter "map_meta_cap"
– I assumed that by indicating 'edit_others_posts'
would have sufficed to bar access of ‘suppliers’ by simple authors (which cannot, in fact, edit other’s posts). I tested this and doesn’t work. Authors can still see all the “suppliers” content in the admin area and add a supplier as if it was a blog post.
How can I prevent authors (or any other specific user role or users without certain capabilities) to access and add content under specific custom post types I’ve created?
thank you
For the solution to your question, In the
register_post_type
arguments, use thecapability_type
parameter & then grant the specific capabilities to the users. For instance, if you set'capability_type' => 'supplier'
, grant theedit_supplier
capability to all administrators onlyMore Details
capabilities
takes an array of the capabilities in the format'edit_post' => 'edit_supplier'
This basically means that wherever the core code was using
edit_post
capability previously, now it will useedit_supplier
(You’ll have to grantedit_supplier
capability to all the users yourself including the administrators, wordpress doesn’t do that for you)if you didn’t provide the capabilities array &
map_meta_cap
is true, then wordpress will generate the default capabilities array from thecapability_type
value provided, like thiswhere
{$capability_type}
is the value you provided. Ifmap_meta_cap
is false, wordpress will ignore thecapability_type
parameter completely (or say consider it to be ‘post’ & then usemap_meta_cap
)