Support

Account

Home Forums Backend Issues (wp-admin) Restricting Access To One Post Type But Not All

Solved

Restricting Access To One Post Type But Not All

  • Hello,

    I am using Members by MemberPress to manage user permissions. I have a custom role built that I’d need to allow to edit certain post types generated by CPT but not others.

    I have 2 CPTs generated by ACF –
    Users
    Lakes

    I’d like to restrict access to Users but enable Access to Lakes.

    Any suggestions?

  • You could create a custom capability and update capabilities for the roles you want to have access to your post type.

    For example, in ACF’s post type creation screen go to the permissions tab. Once there click on “Rename Capabilities”. Add a singular and plural value, in this example I named them “test” and “tests” respectively (I suggest you use something that makes more sense).

    As a test I went ahead and added access to a post type to only the administrator role. In your functions.php file add the following code:

    add_action( 'init', function(){
        $role_object = get_role( 'administrator' );
    
        // add $cap capability to this role object
        $role_object->add_cap( 'YOUR_CAP_VALUE' );
        $role_object->add_cap( 'edit_YOUR_CAP_VALUE' );
        $role_object->add_cap( 'read_YOUR_CAP_VALUE' );
        $role_object->add_cap( 'delete_YOUR_CAP_VALUE' );
    
        $role_object->add_cap( 'edit_YOUR_PLURAL_CAP_VALUE' );
        $role_object->add_cap( 'edit_other_YOUR_PLURAL_CAP_VALUE' );
        $role_object->add_cap( 'publish_YOUR_PLURAL_CAP_VALUE' );
        $role_object->add_cap( 'read_private_YOUR_PLURAL_CAP_VALUE' );
        $role_object->add_cap( 'delete_YOUR_PLURAL_CAP_VALUE' );
        $role_object->add_cap( 'create_YOUR_PLURAL_CAP_VALUE' );
    });

    Make sure to replace both YOUR_CAP_VALUE and YOUR_PLURAL_CAP_VALUE to whatever you pick as the capability names. So in my example they would be “test” and “tests”.

    I suggest checking out the WP documentation on creating post types and the capabilities if you would like more granular control.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.