Apply custom role capabilities to administrator (without plugin)

I have a custom role that allows access to only a sigle custom post type. This is all well and good, but now it is only showing up for this role, not for admin and super admin. I’m having a hard time getting it to show up in the admin dashboard.

add_role( 'artists_relations', 'Artist Relations', array( 'post_artists' ) );
function add_theme_caps() {
    $role = get_role( 'artists_relations');
    $role->add_cap('delete_artists');
    $role->add_cap('delete_published_artists');
    $role->add_cap('delete_others_artists');
    $role->add_cap('edit_artists');
    $role->add_cap('edit_published_artists');
    $role->add_cap('edit_others_artists');
    $role->add_cap('publish_artists');
    $role->add_cap('read');
    $role->add_cap('upload_files');
    $role->add_cap('manage_artist_categories');
}
add_action( 'admin_init', 'add_theme_caps');

works great. I tried many suggestions from the internet, but none worked. The latest attempt, though, looked like this

Read More
function add_admin_caps() {
    $role = get_role( 'administrator');
    $role->add_cap('delete_artists');
    $role->add_cap('delete_published_artists');
    $role->add_cap('delete_others_artists');
    $role->add_cap('edit_artists');
    $role->add_cap('edit_published_artists');
    $role->add_cap('edit_others_artists');
    $role->add_cap('publish_artists');
    $role->add_cap('read');
    $role->add_cap('upload_files');
    $role->add_cap('manage_artist_categories');
}
add_action( 'admin_init', 'add_admin_caps');

I’ve read a few posts that mentioned removing filters. I don’t understand why this would work for a new role but not let me augment an existing one. But isn’t admin supposed to have rights to everything without having to specify it anyway?

— EDIT — (adding post type registration)

function register_artists_post_type() {
    register_post_type('artists',array(
        'labels' => array(
            'name' => __( 'Artists' ),
            'singular_name' => __( 'Artists' ),
            'add_new' => __( 'Add Artist','Artist' ),
            'add_new_item' => __( 'Add New Artist' ),
            'edit_item' => __( 'Edit Artist' ),
            'new_item' => __( 'New Artist' ),
            'view_item' => __( 'View Artist' ),
            'search_items' => __( 'Search Artists' ),
            'not_found' => __( 'No Artists Found' ),
            'not_found_in_trash' => __( 'No Artists In Trash' ),
            'parent_item_colon' => ''
        ),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'has_archive' => true,
        'supports' => array( 'title','editor','excerpt','custom-fields','thumbnail' ),
        'rewrite' => array('slug' => 'artists','with_front' => false),
        'taxonomies' => array('large_feature','small_feature'),
        'capability_type' => 'post',
        'hierarchical' => false,
        'capabilities' => array(
            'publish_posts' => 'publish_artists',
            'edit_posts' => 'edit_artists',
            'edit_others_posts' => 'edit_others_artists',
            'delete_posts' => 'delete_artists',
            'delete_others_posts' => 'delete_others_artists',
            'read_private_posts' => 'read_private_artists',
            'edit_post' => 'edit_artists',
            'delete_post' => 'delete_artists',
            'read_post' => 'read_artists',
            'manage_categories' => 'manage_artist_categories',
        )
    ));
}
add_action('init','register_artists_post_type');

Related posts

Leave a Reply

1 comment

  1. First off, @Wyck is right, it would be helpful to see your register_post_type code. If it were me, I’d make sure I had something like this in the $args array:

    register_post_type( ... array(
        'capability_type' => 'artists',
        'map_meta_cap' => true
    ) );
    

    Next, you don’t want to reset capabilities on every admin page load, that’s unnecessary work for your server. A simple trick I employ is to add ?reload_caps=1 after /wp-admin/ and check for that in my theme’s functions.php file. Here’s some code I used successfully on a site, modified to use the role artists_relations and capability type artists:

    if ( is_admin() && '1' == $_GET['reload_caps'] ) {
        $administrator     = get_role('administrator');
        $artists_relations = get_role('artists_relations');
    
        $administrator->add_cap( 'assign_custom_taxes' );
        $artists_relations->add_cap( 'assign_custom_taxes' );
    
        foreach ( array('publish','delete','delete_others','delete_private','delete_published','edit','edit_others','edit_private','edit_published','read_private') as $cap ) {
            $administrator->add_cap( "{$cap}_artists" );
            $artists_relations->add_cap( "{$cap}_artists" );
        }
    }