wordpresscustom-post-typewordpress-capabilitieswordpress-roles

How to set the 'edit_published_posts' capability of a custom post type on false for a custom role ? Is it even possible?


I try to set up a custom role which has the ability to create posts of a specific custom post type only. This custom role should only be able to create a post, but not publish it. The publishing will be done by an Administrator. After the post is published, it should not be possible for the custom role to edit the post again.

When trying to achieve the above with normal posts (not a custom post type), it works fine. I can just use:

add_role('custom_role', 'My Custom Role', array(
   'read' => true,
   'edit_posts' => true,
   'edit_published_posts' => false,
));

But when trying to archive the above with custom post types, I end up with 2 results depending on how the permissions are set :

  1. I can create and edit posts also after they have been published or
  2. I'm not able to create a post in the first place.

What I tried so far to achieve this:

function my_custom_post_type(){

$args = array(
'labels' => array(
'name' => 'my_custom_post_type',
),
'hierarchical' => false,
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-images-alt2',
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields', 'author'),
'capabilities' => array(
'read_posts' => 'read_my_custom_post_type',
'edit_posts' => 'edit_my_custom_post_type',
'publish_posts' => 'publish_my_custom_post_type',
));

register_post_type('my_custom_post_type', $args);
}
add_action('init', 'my_custom_post_type');

#################

add_role('custom_role', 'custom_role');

#################

function add_role_caps()
{

$role = get_role('custom_role');

$role->add_cap('read_my_custom_post_type', true);
$role->add_cap('edit_my_custom_post_type', true);
$role->add_cap('publish_my_custom_post_type', false);
}

add_action('admin_init', 'add_role_caps', 999);

I also found a page of a WP-Plugin which states that the 'edit_published_posts' capability 'only applies to the “Posts” post type.' https://publishpress.com/knowledge-base/edit_published_posts/

When researching further for this statement, I was not able to find any other sources of this statement.


Solution

  • I found a solution for my Problem:

    First I needed to create a custom capability_type ('Custom_Post', 'Custom_Posts') and then assign the capabilities to the custom role.

    So here is how the working code looks like:

    function my_custom_post_type()
    {
    
      $args = array(
        'labels' => array(
          'name' => 'my_custom_post_type',
        ),
        'hierarchical' => false,
        'public' => true,
        'has_archive' => true,
        'menu_icon' => 'dashicons-images-alt2',
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields', 'author'),
        'capability_type' => array('Custom_Post', 'Custom_Posts'),
        'map_meta_cap'    => true,
      );
    
      register_post_type('my_custom_post_type', $args);
    }
    add_action('init', 'my_custom_post_type');
    
    #################
    
    add_role('custom_role', 'custom_role');
    
    #################
    
    function add_role_caps()
    {
    
      $role = get_role('custom_role');
    
      $role->add_cap('read_Custom_Post', true);
      $role->add_cap('edit_Custom_Post', true);
      $role->add_cap('publish_Custom_Posts', false);
      $role->add_cap('edit_published_Custom_Posts', false);
    }
    
    add_action('admin_init', 'add_role_caps', 999);