• Resolved jabedbd

    (@jabedbd)


    Hello, i’m facing a strange issue in WordPress.

    I have created a custom post type with

    
    "capability_type" => array('my_post','my_posts'),
    "map_meta_cap" => true,

    And then create custom role, and add the capabililities this to the role so that user in that role can access the custom post type:

    $role = get_role('my_role');
    
        // This only works, because it accesses the class instance.
        // would allow the subscriber to edit others' posts for current theme only
        $role->add_cap('read');
        $role->add_cap('read_my_post');
        $role->add_cap('delete_my_post' );
        $role->add_cap('edit_my_posts');
        $role->add_cap('edit_others_my_posts' );
        $role->add_cap('delete_my_posts' );
        $role->add_cap('publish_my_posts' );
        $role->add_cap('read_private_my_posts');
        $role->add_cap('delete_private_my_posts');
        $role->add_cap('delete_published_my_posts' );
        $role->add_cap('delete_others_my_posts' );
        $role->add_cap('edit_private_my_posts' );
        $role->add_cap('edit_published_my_posts' );
        $role->add_cap('edit_my_posts' );

    Now when i logged in a user with the “my_role” role, i can see the post type in WordPress menu, but when i click on it (to open the post list page from where i can edit/delete/add post) it’s showing me “Sorry you are not allowed to access this page”.

    • This topic was modified 4 years, 5 months ago by jabedbd.
    • This topic was modified 4 years, 5 months ago by jabedbd. Reason: spelling mistake
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    When you register the post type, use the “capabilities” arg to explicitly map each *_post? cap to the equivalent *_my_post? cap. Seems like it shouldn’t be necessary with “capability_type” and “map_mata_cap” args, but I’ve run into the same issue you have and that solved it. For example:

    'capabilities' => array(
    	'edit_post' => 'edit_my_post',
    	'read_post' => 'read_my_post',
    	'delete_post' => 'delete_my_post',
    	//etc. for each cap
    ),
    Thread Starter jabedbd

    (@jabedbd)

    @bcworkz thanks for your reply. i just tried your way. but facing the same issue 🙁

    • This reply was modified 4 years, 5 months ago by jabedbd.
    Moderator bcworkz

    (@bcworkz)

    Are you able to post your post type registration code for review?

    Thread Starter jabedbd

    (@jabedbd)

    @bcworkz Hello, the issue is fixed. the issue i was using “create_posts” => “do_not_allow” so that user can’t create posts.
    But seems like wordpress do not allow to see the list who don’t have permission to create post.
    i think there should be a option to disable the Add New button (create post option) but still allow user to see the post lists from dashboard.

    Moderator bcworkz

    (@bcworkz)

    Huh, doing that on my test site’s CPT works as we’d expect — user can list, delete, and edit posts, but cannot create any new ones. There are no Add New buttons or links that I could find. If your back end pages are cached you might be seeing stale content with buttons that don’t belong there.

    While mapping “create_posts” to “do_not_allow” should serve the intended purpose, it means giving a “do_not_allow” cap to a user who should be able to create new posts. It strikes me as rather confusing. It’ll do to be more conventional and map to “create_my_posts” and simply not assign that cap to the role.

    Thread Starter jabedbd

    (@jabedbd)

    @bcworkz sure, i will try that. btw, as my current issue fixed i’m makring this issue as solved. thanks a lot for your help.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Custom Post Permission Issue with Custom Role’ is closed to new replies.