WordPress custom post type capabilities, admin can’t edit post type

I’m having a wired problem with WordPress. Below is my code for an events post type, it works without the capabilities but when the capabilities are added all the default roles (admin, editor, etc…) cant use the post type. The admin role is only able to see the custom taxonomies.

I have a custom user role with “edit_events => true” for the user role that is able to submit events for review. This is what I want, but the built in roles can’t see the post type!

Read More

I’ve tried just about every capabilities tutorial I could find, they all seem to be pretty much the same and I can’t how any of them differ from my code.

function register_custom_event_type() {
$labels = array(
    'name' => _x('Events', 'event'),
    'singular_name' => _x('Event', 'event'),
    'add_new' => _x('Add New', 'event'),
    'add_new_item' => _x('Add New Event', 'event'),
    'edit_item' => _x('Edit Event', 'event'),
    'new_item' => _x('New Event', 'event'),
    'view_item' => _x('View Event', 'event'),
    'search_items' => _x('Search Events', 'event'),
    'not_found' => _x('No events found', 'event'),
    'not_found_in_trash' => _x('No events found in Trash', 'event'),
    'parent_item_colon' => _x('Parent Event:', 'event'),
    'menu_name' => _x('Events', 'event'),
);

$args = array(
    'labels' => $labels,
    'hierarchical' => false,
    'supports' => array('title', 'editor', 'thumbnail', 'author'),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capability_type' => 'event',
    'capabilities' => array(
            'read_post' => 'read_event',
            'publish_posts' => 'publish_events',
            'edit_posts' => 'edit_events',
            'edit_others_posts' => 'edit_others_events',
            'delete_posts' => 'delete_events',
            'delete_others_posts' => 'delete_others_events',
            'read_private_posts' => 'read_private_events',
            'edit_post' => 'edit_event',
            'delete_post' => 'delete_event',

        ),
    'map_meta_cap' => true
);
register_post_type('event', $args);
}
add_action('init', 'register_custom_event_type');

Related posts

Leave a Reply

6 comments

  1. I found a work around in the end.

    I thought the default WordPress Roles would have the same capabilities for the post type as a normal post, but for some reason they don’t.

    Adding the capabilities manually seems to work.

    function add_event_caps() {
      $role = get_role( 'administrator' );
      $role->add_cap( 'edit_event' ); 
      $role->add_cap( 'edit_events' ); 
      $role->add_cap( 'edit_others_events' ); 
      $role->add_cap( 'publish_events' ); 
      $role->add_cap( 'read_event' ); 
      $role->add_cap( 'read_private_events' ); 
      $role->add_cap( 'delete_event' ); 
      $role->add_cap( 'edit_published_events' );   //added
      $role->add_cap( 'delete_published_events' ); //added
    }
    add_action( 'admin_init', 'add_event_caps');
    
  2. make sure to include
    'map_meta_cap' => true, when registering your custom post type also set the capability type correctly eg. 'capability_type' => array('event', 'events'), then use the User Role Editor Plugin and click the permissions as you see fit for the individual roles – done!

  3. After setting 'map_meta_cap' => true and setting 'capability_type' => array('event', 'events') you might also need to add the capabilities:

    There are also eight other primitive capabilities which are not referenced directly in core, except in map_meta_cap(), which takes the three aforementioned meta capabilities and translates them into one or more primitive capabilities that must then be checked against the user or role, depending on the context. These additional capabilities are only used in map_meta_cap(). Thus, they are only assigned by default if the post type is registered with the ‘map_meta_cap’ argument set to true (default is false).

    read - Controls whether objects of this post type can be read.
    delete_posts - Controls whether objects of this post type can be deleted.
    delete_private_posts - Controls whether private objects can be deleted.
    delete_published_posts - Controls whether published objects can be deleted.
    delete_others_posts - Controls whether objects owned by other users can be can be deleted. If the post type does not support an author, then this will behave like delete_posts.
    edit_private_posts - Controls whether private objects can be edited.
    edit_published_posts - Controls whether published objects can be edited.
    create_posts - Controls whether new objects can be created
    

    register_post_type() WordPress Codex

  4. thats a strange problem. it should work. the following will work for you (note i just copied paste my code so some of my code creeped in)

    add_action('init', 'register_custom_event_type');
    function register_custom_event_type() {
      register_post_type('event',
        array(
        'labels' => array(
          'name' => _x('Event', 'Event'),
          'singular_name' => _x('Event Custom Post', 'domain'),
          'add_new' => _x('Add New', 'domain'),
          'add_new_item' => _x('Add New Event Custom Post', 'domain'),
          'edit' => _x('Edit', 'domain'),
          'edit_item' => _x('Edit Event Custom Post', 'domain'),
          'new_item' => _x('New Event Custom Post', 'domain'),
          'view' => _x('View Event Custom Post', 'domain'),
          'view_item' => _x('View Event Custom Post', 'domain'),
          'search_items' => _x('Search Event Custom Post', 'domain'),
          'not_found' => _x('No Event Custom Posts found', 'domain'),
          'not_found_in_trash' => _x('No Event Custom Posts found in Trash', 'domain')
        ),
        'public' => true,
        'hierarchical' => true,
        'has_archive' => true,
        'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
        'can_export' => true,
        'taxonomies' => array( 'category' ),
        'register_meta_box_cb' => '',
        'map_meta_cap' => true,
        'capability_type' => 'event',
        'capabilities' => array(
          'read_post' => 'read_event',
          'publish_posts' => 'publish_events',
          'edit_posts' => 'edit_events',
          'edit_others_posts' => 'edit_others_events',
          'delete_posts' => 'delete_events',
          'delete_others_posts' => 'delete_others_events',
          'read_private_posts' => 'read_private_events',
          'edit_post' => 'edit_event',
          'delete_post' => 'delete_event',
         ),
      ));
    }
    

    Something to do with the way you are calling it. Dont know where. sometimes its easier just to start again 🙂

    Good Luck

  5. Had the same issue. Custom capability_type in custom post type, logged in as user with new user role is no problem, but the admin did not see the articles in CPT.

    Installing “User Role Editor” did the trick. Acivating, Reload, Deactivating, Deleting. Still works.

  6. When registering the post type, add the supports tag enabling editor like this:
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt' )