Wordpress custom post type capabilities, admin can't edit post type

谁说我不能喝 提交于 2020-06-25 05:46:30

问题


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!

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');

回答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:


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



来源:https://stackoverflow.com/questions/18324883/wordpress-custom-post-type-capabilities-admin-cant-edit-post-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!