Custom post type capabilities require “create_posts” to access the edit posts list page

I have a custom post type and a custom user role that I want to be able to do everything but delete and create this post type. However, this user role can’t view the dashboard’s list of posts for editing when I do not set the “create_{$post_type}” capability. Instead they see a white screen error message saying “Sorry, you are not allowed to access this page.” My code is below.

What do I need to add or change so that the Logistics user can edit others’ posts but not create them?

add_action( 'init', 'register_wsorder_post_type' );
function register_wsorder_post_type () {

  // Post type labels.
  $labels = array(
    'name'               => 'Orders',
    'singular_name'      => 'Order',
    'add_new'            => __( 'Add New', 'textdomain' ),
    'add_new_item'       => __( 'Add New', 'textdomain' ) . " Order",
    'edit_item'          => __( 'Edit', 'textdomain' ) . " Order",
    'new_item'           => __( 'New', 'textdomain' ) . " Order",
    'view_item'          => __( 'View', 'textdomain' ) . " Order",
    'search_items'       => __( 'Search', 'textdomain' ) . " Orders",
    /* translators: placeholder is the plural taxonomy name */
    'not_found'          => sprintf( esc_html__( 'No %d Found', 'textdomain' ), 'Orders' ),
    /* translators: placeholder is the plural taxonomy name */
    'not_found_in_trash' => sprintf( esc_html__( 'No %d found in trash', 'textdomain' ), 'Orders' ),
    'parent_item_colon'  => '',
    'menu_name'          => 'Orders',
  );

  // Post type arguments.
  $args = array(
    'can_export'         => true,
    'has_archive'        => true,
    'labels'             => $labels,
    'menu_icon'          => 'dashicons-portfolio',
    'menu_position'      => 20,
    'public'             => true,
    'publicly_queryable' => true,
    'show_in_rest'       => true,
    'show_in_menu'       => true,
    'show_in_admin_bar'  => true,
    'show_in_nav_menus'  => true,
    'show_ui'            => true,
    'supports'           => rray( 'title', 'author' ),
    'rewrite'            => array(
      'with_front' => false,
      'slug'       => 'wsorder',
    ),
    'capability_type' => 'wsorder',
    'capabilities' => array(
      'edit_post'                 => 'edit_wsorder',
      'read_post'                 => 'read_wsorder',
      'delete_post'               => 'delete_wsorder',
      'create_posts'              => 'create_wsorders',
      'delete_posts'              => 'delete_wsorders',
      'delete_others_posts'       => 'delete_others_wsorders',
      'delete_private_posts'      => 'delete_private_wsorders',
      'delete_published_posts'    => 'delete_published_wsorders',
      'edit_posts'                => 'edit_wsorders',
      'edit_others_posts'         => 'edit_others_wsorders',
      'edit_private_posts'        => 'edit_private_wsorders',
      'edit_published_posts'      => 'edit_published_wsorders',
      'publish_posts'             => 'publish_wsorders',
      'read_private_posts'        => 'read_private_wsorders'
    ),
    'map_meta_cap' => true,
  );

  // Register the post type.
  register_post_type( 'wsorder', $args );

});

register_activation_hook( __FILE__, 'activate_plugin' );
function activate_plugin(){
  
  $caps = array(
    'edit_wsorder'              => true,
    'read_wsorder'              => true,
    'create_wsorders'           => false, // I don't want to enable this but it's needed to edit others orders for some reason.
    'edit_wsorders'             => true,
    'edit_others_wsorders'      => true,
    'edit_private_wsorders'     => true,
    'edit_published_wsorders'   => true,
    'publish_wsorders'          => true,
    'read_private_wsorders'     => true,
    'read'                      => true,
  );
  add_role( 'logistics', 'Logistics', $caps );

}

register_deactivation_hook( __FILE__, 'deactivate_plugin' );
function deactivate_plugin(){
  
  remove_role( 'logistics' );

}

0

Leave a Comment