Create user role restricted to specific CPT

I am trying to create a role that can create and submit to a CPT only. When I take away edit_post from the role, it revokes the ability to create and edit any of the assigned and registered CPTs as well. I have registered and assigned the edit_CPTs capability to the role as well. So basically, I have this:

edit_cpt
edit_cpts

and I have taken away

edit_posts

But once I remove edit_posts the role cannot even see the CPTs in the dashboard interface. Any help would be greatly appreciated.

1 Answer
1

You need to register the CPT with your custom capability, then assign that specific cap to the user.

When passing the arguments to register_post_type, set capability_type to your new capability, so the check turns into 'edit_cpts' instead of 'edit_post'.

By setting 'capability_type' => [ 'cpt', 'cpts' ], ‘cpt’ will map to ‘post’ for standard capability checks, so where you’d usually check for ‘edit_post’ you’ll now check for ‘edit_cpt’.

When you completely remove the primitive 'edit_posts' cap from users/roles as you’ve done, you’ll need to add in your new one with WP_Role::add_cap() and check for it with current_user_can( 'edit_cpts' ).

Leave a Comment