I hit an issue with an existing event plugin on a client’s website that created a post type but didn’t distinguish “edit posts” and “create new post” capabilities
This is the original capability mapping of the post type “event”, you will notice create_posts and edit_posts are mapped to the same custom user role.
[edit_post] => edit_event [read_post] => read_event [delete_post] => delete_event [edit_posts] => edit_events [edit_others_posts] => edit_others_events [publish_posts] => publish_events [read_private_posts] => read_private_events [delete_posts] => delete_events [delete_others_posts] => delete_others_events [create_posts] => edit_events
9/10 times this good enough for most use cases, I managed to come across that rare scenario were i needed to separate the two capabilities.
So how do you modify the custom roles of an existing post type? It ended up being relatively simple.
Using WordPress’ get_post_type_object function to load up the existing post type you can see the existing setup.
// Print out the existing post type setup print_r(get_post_type_object("your_post_type"));
The output will show the post type’s mapped capabilities under the cap object array, with this knowledge you can modify the variable you’re after then initiate the changes using the wordpress add_action function.
For example
function modify_post_type(){ // Get the post type $event_post_type = get_post_type_object("event"); $caps = $event_post_type->cap; // Set the new custom capability map for the "create_posts" functionality of this post_type $caps->create_posts="create_events"; } // Now you just need to Grant the new capability to which ever user role you wish add_action("init", "modify_post_type");
Now in a user role editor you should see the following, any user not granted the create_events (create_posts) capability will no longer be able see the Add New button.
The new cap output
[edit_post] => edit_event [read_post] => read_event [delete_post] => delete_event [edit_posts] => edit_events [edit_others_posts] => edit_others_events [publish_posts] => publish_events [read_private_posts] => read_private_events [delete_posts] => delete_events [delete_others_posts] => delete_others_events [create_posts] => create_events