• Resolved jenry

    (@jenry)


    Hi all,

    I’ve created a custom post type “reservations”, which I only want to be accessible by users with the custom capabilities set for the post type.

    I created a role named “Reservation Manager” where I have added the capability to both this role and the “administrator” role to have access to the post type to do edits, deletes, publish etc. I created one capability name “manage_reservations” to handle all of this, via post type “capabilities” arg.

    With the “Reservation Manager”, I can do edit, deletes, etc, everything except when I click the “Add New” button, it says, “Sorry, you are not allowed to access this page.”.

    If I add the “edit_posts” capability to the “Reservation Manager” role, its fine, I get to do the “Add New” stuff. If i also set the “show_in_menu” args to “true”, I can “Add New” posts for the post type, even without adding the “edit_posts” capability to the “Reservation Manager” role.

    What am I doing wrong? Everything works fine except “Add New” post for the custom post type.

    See below for code snippet of post type args. Please, any help….

    // get args
    $args = [
    	'exclude_from_search' => true,
    	'publicly_querable' => false,
    	'show_ui' => true,
    	'show_in_nav_menus' => false,
    	'show_in_menu' => false,
    	'show_in_admin_bar' => true,
    	'map_meta_cap' => true,
    	'capability_type' => 'reservation',
    	'capabilities' => [
    		// meta caps (don't assign these to roles)
    		'edit_post'              => 'edit_reservation',
    		'read_post'              => 'read_reservation',
    		'delete_post'            => 'delete_reservation',
    		// primitive/meta caps
    		'create_posts'           => 'manage_reservations',
    		// primitive caps used outside of map_meta_cap()
    		'edit_posts'             => 'manage_reservations',
    		'edit_others_posts'      => 'manage_reservations',
    		'publish_posts'          => 'manage_reservations',
    		'read_private_posts'     => 'read',
    		// primitive caps used inside of map_meta_cap()
    		'read'                   => 'read',
    		'delete_posts'           => 'manage_reservations',
    		'delete_private_posts'   => 'manage_reservations',
    		'delete_published_posts' => 'manage_reservations',
    		'delete_others_posts'    => 'manage_reservations',
    		'edit_private_posts'     => 'manage_reservations',
    		'edit_published_posts'   => 'manage_reservations',
    	],
    ];
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Nothing wrong per se, more of an odd WP quirk. For whatever reason, WP in part checks a user’s ability to access a menu item in order to decide if the related screen should be displayed. Since the add new menu item does not exist, WP will refuse to load the new post edit screen.

    So to add a new CPT, the add new item must appear on the admin menu. Could you do 'show_in_menu' => true,, then hide it with CSS as a workaround? Why is it important that the CPT not appear in the menu? It is only displayed for those with the proper capability anyway.

    Thread Starter jenry

    (@jenry)

    Thanks for your reply.

    show_in_menu is set to false, as I have the custom post type linked as a submenu page to a plugin’s main menu.

    Thread Starter jenry

    (@jenry)

    I found a hack mate, thanks to you saying the menu item for add new does not exist, which is why wordpress is not allowing me to create posts. So it got me thinking, what hack can I do to make wordpress think its there without it actually displaying?

    Well, after hours of thinking and trying all sort of things, it finally hit me to dump the $GLOBALS and see what stuff is there that I can manipulate (thanks to symfony dump() function, it was a breeze understanding what all is there).

    I used the “admin_menu” hook, and got the global $submenu variable and edited it so that wordpress now sees the add new menu, although it is not displayed.

    $submenu['edit.php?post_type=reservations'][] = [
    	'Add Reservations',
    	'', // leave permission empty so it doesn't display
    	'post-new.php?post_type=reservations',
    	'Add Reservations',
    ];
    • This reply was modified 7 years, 5 months ago by jenry.
    • This reply was modified 7 years, 5 months ago by jenry.
    • This reply was modified 7 years, 5 months ago by jenry.
    • This reply was modified 7 years, 5 months ago by jenry.
    Moderator bcworkz

    (@bcworkz)

    ?? Brilliant, well done!

    Thread Starter jenry

    (@jenry)

    Thanks as always.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘User cannot “Add New” for Custom Post Type’ is closed to new replies.