Allow Editor to viewmodify a custom post type

admin2025-01-07  3

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 );
}

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 );
}
Share Improve this question asked Nov 29, 2018 at 13:30 bonesbones 1111 silver badge3 bronze badges 5
  • Adding a custom role check :codex.wordpress.org/Function_Reference/add_role – vikrant zilpe Commented 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 Commented 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 Commented 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 Commented 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 Commented Nov 30, 2018 at 9:42
Add a comment  | 

1 Answer 1

Reset to default 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 );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736264529a975.html

最新回复(0)