I am currently working on making a custom user role "adoption_agency". Along with this custom role, i have created a custom post type called "adoptions". I have given "administrators" and "shop_manager" full access to this post type, which should allow them to delete, update or edit the post. When I login as an "adoption_agency" , "shop_manager" or "administrator" user I am able to view the custom post type on my dashbaord and i am able to access it. The issue i am having is that when I go to add new adoption and publish it, it says "You are not allowed to edit this post." I would like for the "adoption_agency" users to only have the "adoptions" post type available on their dashboards. I have added my code below, I have been trying for a few days now and i cant seem to get it working. I would really appreciate the help.
Here is the my custom post type code:
// Register Custom Post Type
function custom_post_type_adoption() {
$labels = array(
'name' => _x( 'adoptions', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'adoption', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Adoptions', 'text_domain' ),
'name_admin_bar' => __( 'Adoptions', 'text_domain' ),
'archives' => __( 'Adoption Archives', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Adoptions', 'text_domain' ),
'add_new_item' => __( 'Add New Adoption', 'text_domain' ),
'add_new' => __( 'Add New Adoption', 'text_domain' ),
'new_item' => __( 'New Adoption', 'text_domain' ),
'edit_item' => __( 'Edit Adoption', 'text_domain' ),
'update_item' => __( 'Update Adoption', 'text_domain' ),
'view_item' => __( 'View Adoption', 'text_domain' ),
'search_items' => __( 'Search Adoption', 'text_domain' ),
'not_found' => __( 'Adoption Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Adoption Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into Adoption', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this Adoption', 'text_domain' ),
'items_list' => __( 'Adoption list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
);
$args = array(
'label' => __( 'adoption', 'text_domain' ),
'description' => __( 'Post type for Adoptions', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'excerpt', 'author', 'thumbnail', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-smiley',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => array("adoption", "adoptions"),
);
register_post_type( 'adoptions', $args );
}
add_action( 'init', 'custom_post_type_adoption', 0 );
Here is my custom user role code:
function add_adoption_caps_to_admin() {
$caps = array(
'read',
'read_adoptions',
'read_private_adoptions',
'edit_adoptions',
'edit_private_adoptions',
'edit_published_adoptions',
'edit_others_adoptions',
'publish_adoptions',
'delete_adoptions',
'delete_private_adoptions',
'delete_published_adoptions',
'delete_others_adoptions',
'upload_files',
'edit_files',
);
$roles = array(
get_role( 'administrator' ),
get_role( 'editor' ),
get_role( 'shop_manager' ),
);
foreach ($roles as $role) {
foreach ($caps as $cap) {
$role->add_cap( $cap );
}
}
}
add_action( 'after_setup_theme', 'add_adoption_caps_to_admin' );
function adoption_agency_setup() {
add_role( 'adoption_agency', 'Adoption Agency');
}
add_action( 'after_setup_theme', 'adoption_agency_setup' );
function add_adoption_caps_to_non_admin_roles() {
# Everyone gets these capabilities:
$caps = array(
'read',
'read_adoptions',
'read_private_adoptions',
'edit_adoptions',
'edit_published_adoptions',
'publish_adoptions',
'delete_adoptions',
'delete_published_adoptions',
'upload_files',
'edit_files',
);
$roles = array(
get_role( 'adoption_agency' ),
);
foreach ($roles as $role) {
foreach ($caps as $cap) {
$role->add_cap( $cap );
}
}
}
add_action( 'after_setup_theme', 'add_adoption_caps_to_non_admin_roles' );
function custom_remove_menus(){
// Get current user's data
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
// Check user's roles
$user = new WP_User( $user_id );
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
if( in_array( 'adoption_agency', $user->roles ) ) {
// Remove menu items
remove_menu_page( 'upload.php');
remove_menu_page( 'index.php');
}
}
}
add_action( 'admin_menu', 'custom_remove_menus' );
capability_type
. Check that argument on this page: codex.wordpress.org/Function_Reference/…$GLOBALS['wp_post_types']['adoptions']
?