Create user role restricted to specific CPT

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