admin管理员组

文章数量:1391751

I have a custom post type created using the following code:

// Event Post type
register_post_type('event', array(
  'capability_type' => 'event',
  'map_meta_cap' => true,
  'supports' => array('title', 'editor', 'excerpt', 'revisions'),
  'rewrite' => array('slug' => 'events'),
  'has_archive' => true,
  'public' => true,
  'labels' => array(
    'name' => 'Events',
    'add_new_item' => 'Add new event',
    'new_item' => 'New Event',
    'edit_item' => 'Edit events',
    'all_items' => 'All events',
    'singular_name' => 'Event'
  ),
  'menu_icon' => 'dashicons-calendar',
  'taxonomies' => array( 'category' )
));

I also have a member role called editor which gets all the capabilities (using Justin Tadlock's/Memberspress plugin):

However, he cannot change the category of new or old event posts. I have found a weird workaround and it is to add the capability "Edit Posts" to that role, which is of course not a capability I wish him to have.

I would love to understand what causes this issue and how to solve it.

I have a custom post type created using the following code:

// Event Post type
register_post_type('event', array(
  'capability_type' => 'event',
  'map_meta_cap' => true,
  'supports' => array('title', 'editor', 'excerpt', 'revisions'),
  'rewrite' => array('slug' => 'events'),
  'has_archive' => true,
  'public' => true,
  'labels' => array(
    'name' => 'Events',
    'add_new_item' => 'Add new event',
    'new_item' => 'New Event',
    'edit_item' => 'Edit events',
    'all_items' => 'All events',
    'singular_name' => 'Event'
  ),
  'menu_icon' => 'dashicons-calendar',
  'taxonomies' => array( 'category' )
));

I also have a member role called editor which gets all the capabilities (using Justin Tadlock's/Memberspress plugin):

However, he cannot change the category of new or old event posts. I have found a weird workaround and it is to add the capability "Edit Posts" to that role, which is of course not a capability I wish him to have.

I would love to understand what causes this issue and how to solve it.

Share Improve this question edited Feb 7, 2020 at 10:02 CaptainNemo asked Feb 7, 2020 at 8:31 CaptainNemoCaptainNemo 1054 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

CPT capabilities do not cover taxonomies. Each taxonomy has its own capabilities. If you want to use default Categories, unfortunately you must give user "Edit Posts" capability. There is no easy way around it, especially with Gutenberg. Best solution would be to create custom taxonomy "Event Categories" with matching capability requirements, for example, mapping all taxonomy caps to "edit_events".

From what I can see on a first glance, you have a single-quote missing:

'name' => 'Events,

should be:

'name' => 'Events',

In addition, your declaration of the Custom Post Type is incomplete as far as I can see (and pretty ugly formatted).

Try something like this:

<?php

/* Register Custom Post Type 'events' */
function prefix_register_custom_post_type(){

// Custom Post Type Name
$cpt_name = 'events';
// CPT Features/possible values:
// 'title', 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'
$cpt_features = array(
    'title',
    'revisions',
    'thumbnail',
    'editor'
);
// Slug
$cpt_slug = 'events'; // What the URL will look like
$labels = array(
    'name'                      =>  __('Events', 'text-domain'),
    'singular_name'             =>  __('Event', 'text-domain'),
    'menu_name'                 =>  __('Events', 'text-domain'),
    'name_admin_bar'            =>  __('Events', 'text-domain'),
    'all_items'                 =>  __('All events', 'text-domain'), // Archive Page Name
    'add_name'                  =>  __('Add new event', 'text-domain'),
    'add_new_item'              =>  __('Add new event', 'text-domain'),
    'edit'                      =>  __('edit event', 'text-domain'),
    'edit_item'                 =>  __('edit event', 'text-domain'),
    'new_item'                  =>  __('New event', 'text-domain'),
    'view'                      =>  __('View event', 'text-domain'),
    'view_item'                 =>  __('View event', 'text-domain'),
    'search_items'              =>  __('Search ', 'text-domain'),
    'parent'                    =>  __('Parent', 'text-domain'),
    'not_found'                 =>  __('No events found', 'text-domain'),
    'not_found_in_trash'        =>  __('No events found in Trash', 'text-domain')
);

/* ------------------------------------------ End of Edit */
$args = array(
    'labels'                =>  $labels,
    'public'                =>  true,
    'publicly_queryable'    =>  true,
    'exclude_from_search'   =>  false,
    'show_in_nav_menus'     =>  true,
    'show_ui'               =>  true,
    'show_in_menu'          =>  true,
    'show_in_admin_bar'     =>  true,
    'menu_position'         =>  21,
    'menu_icon'             =>  'dashicons-awards',
    'can_export'            =>  true,
    'delete_with_user'      =>  false,
    'hierarchical'          =>  true,
    'has_archive'           =>  true,
    'query_var'             =>  true,
    'capability_type'       =>  'post',
    'map_meta_cap'          =>  true,
    // 'capabilities'       => array(),
    'rewrite'               =>  array(
        'slug'      => $cpt_slug,
        'with_front'=> true,
        'pages'     => true,
        'feeds'     => false
    ),
    'supports'      => $cpt_features
);

register_post_type($cpt_name, $args);

}
add_action('init', 'prefix_register_custom_post_type');

Since this formatting is a bit easier to read, please check or replace your code with this and try again. This code usually allows "editors" to access and manipulate the registered custom post types. Also see the commented "capabilities" array where you can enter things like 'edit_posts'.

本文标签: Unable to edit categories in custom post type