1

I created three new custom post types and they all work fine for my admin user. A User with the Editor role can see the menu items in the admin, but when they click one it says:

"Sorry, you are not allowed to access this page."

It's been driving me crazy, any idea what I am missing?

// The custom function MUST be hooked to the init action hook
add_action( 'init', 'handbook_noodles_post_type', 0 );

// A custom function that calls register_post_type
function handbook_noodles_post_type()
{
    // Set various pieces of text, $labels is used inside the $args array
    $labels = array(
        'name' => 'Noodles\'s Handbook',
        'singular_name' => 'Noodles\'s Page',
        'add_new' => __( 'Add New Page' ),
        'add_new_item' => 'Add New Page',
        'edit_item' => 'Edit Page',
    );

    // Set various pieces of information about the post type
    $args = array(
        'labels' => $labels,
        'description' => 'Noodles\'s Handbook Pages',
        'public' => true,
        'exclude_from_search' => true,
        'query_var' => false,
        'show_in_menu' => 'employee_handbook.php',
        'supports' => array('title', 'editor', 'page-attributes'),
        'hierarchical' => true,
        'has_archive' => true,
        'capability_type' => 'post',
        'map_meta_cap' => true
    );

    register_post_type( 'noodles-handbook', $args );
}
5
  • Adding a custom role check :codex.wordpress.org/Function_Reference/add_role Nov 29, 2018 at 13:36
  • Are the editors capable of editing regular posts or do they get the same message? Since you've given your CPT the capability type of 'post' this should also occur on posts. Either way this shouldn't be the csae
    – Jebble
    Nov 29, 2018 at 13:41
  • @JeffreyvonGrumbkow yeah they can edit posts no problem. That's why it's driving me crazy. If I do a get_role('editor') everything seems to be in order.
    – bones
    Nov 29, 2018 at 13:46
  • I've discovered that if I use show_in_menu = true then it works, if I try to create a unique menu and put my three custom post types in that using 'show_in_menu' => 'myuniqueslug', then I see the menu fine but I can't access the pages unless I'm an admin.
    – bones
    Nov 30, 2018 at 3:38
  • Right, I've never made a custom admin page that shows multiple CPT's in one page and then hide the original ones. Maybe keep them in the menu and hide them with CSS?
    – Jebble
    Nov 30, 2018 at 9:42

1 Answer 1

0

I suspect the problem is not when you register the post type but when you create the menu pages.

When you call add_submenu_page() make sure that the $capability argument is correct. In your case you want 'edit_posts':

$capability = 'edit_posts';
add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.