4

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

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');
1
  • It would be useful to show your register post type
    – Wyck
    Dec 4, 2012 at 3:08

1 Answer 1

5

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" );
    }
}
6
  • Thanks, I've added the registration. I guess I didn't wholly grasp what the capability_type really meant. I thought if it was post then it would behave like a post. Re-reading the codex just makes me more confused :( Unfortunately, though, making that change didn't cause it to show up in admin view. Dec 4, 2012 at 17:42
  • Also, what do you mean by reset capabilities? What in this code "resets" it? Dec 4, 2012 at 17:44
  • Running add_cap is only necessary to do once, not on every page load. So what we've done here is, if reload_caps=1 is in the URL, we're running all the commands. Otherwise, we're not running them unnecessarily. So you added all this, and then went to http://your-site.com/wp-admin/?reload_caps=1? Dec 4, 2012 at 17:47
  • Yes, and still no artist tab in the sidebar. Blarg. Dec 4, 2012 at 20:42
  • Nevermind, the file wasn't updating for some reason! Yes, this works beautifully, thank you so much. For anyone else coming across this, all you need is this code and the single line add_role( 'artists_relations', 'Artist Relations', array( 'post_artists' ) ); to register the role. No capabilities array in register_post_type and add_theme_caps isn't needed at all. Again, Matthew, thank you for such an elegant solution! Best I've seen yet. I did have to add read to the array, though :) Dec 4, 2012 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.