1

I have a custom post type which I want authors to be able to edit others posts but no able to delete others posts. My code as follows:

$labels = array(
    'name'          => __( 'Book', 'textdomain' ),
    'singular_name' => __( 'Book', 'textdomain' ),
    'menu_name'     => __( 'Books', 'textdomain' ),
    'add_new'       => __( 'New book', 'textdomain' ),
    'add_new_item'  => __( 'New book', 'textdomain' ),
    'edit_item'     => __( 'Edit book', 'textdomain' ),
);
$args = array(
    'labels'        => $labels,
    'public'        => true,
    'menu_position' => 6,
    'menu_icon'     => 'dashicons-book',
    'has_archive'   => true,
    'supports'      => array( 'title', 'author' ),
    'capabilities'  => array(
        'edit_post'             => 'edit_posts',
        'read_post'             => 'edit_posts',
        'delete_post'           => 'edit_posts',
        'edit_posts'            => 'edit_posts',
        'edit_others_posts'     => 'edit_posts',
        'publish_posts'         => 'edit_posts',
        'read_private_posts'    => 'edit_others_posts',
        'delete_others_posts'   => 'edit_others_posts',
    ),
);
register_post_type( 'book', $args );

What I am experiencing with the above code is:

  1. When 'edit_others_posts' => 'edit_posts', "mine" tab in admin post editor list doesn't show, and I want it to be always shown. Is it possible to force it back?
  2. Even if 'delete_others_posts' => 'edit_others_posts' (author users don't have the capability to 'edit_others_posts'), authors can delete others posts.
  3. If I modify the code above with 'edit_others_posts' => 'edit_others_posts', "mine" tab shows up but author users still can edit others posts, which confuse me.

I'm really not sure if I am using capabilities the right way or if what I intent to do is possible. Thanks in advance.

1

Ok - lets try this again :)

I added the map_meta_cap parameter which is needed to have delete_others_posts work. Now I was able to get authors to edit and view all posts but only delete their own.

$labels = array(
    'name'          => __( 'Book', 'textdomain' ),
    'singular_name' => __( 'Book', 'textdomain' ),
    'menu_name'     => __( 'Books', 'textdomain' ),
    'add_new'       => __( 'New book', 'textdomain' ),
    'add_new_item'  => __( 'New book', 'textdomain' ),
    'edit_item'     => __( 'Edit book', 'textdomain' ),
);
$args = array(
    'labels'        => $labels,
    'public'        => true,
    'menu_position' => 6,
    'menu_icon'     => 'dashicons-book',
    'has_archive'   => true,
    'supports'      => array( 'title', 'author' ),
    'map_meta_cap'  => true,
    'capabilities'  => array(
        'read_post' => 'read_post',
        'edit_post' => 'edit_posts',
        'edit_posts' => 'edit_posts',
        'edit_others_posts'     => 'edit_posts',
        'delete_others_posts'   => 'install_plugins',
    )

);
register_post_type( 'book', $args );
  • Thanks. It's not exactly what I'm trying to accomplish here. If 'delete_post' => 'install_plugins' only admins are allowed to delete posts. What I want to achieve is authors being able to edit others posts, delete their own posts, but not able to delete others posts. Quite complicate. Maybe not possible I'm guessing. Codex isn't helping here. – vguerrero Jul 24 '14 at 14:30
  • See re-edited answer. – Welcher Jul 24 '14 at 20:24
  • Thank you very very much. All capabilities working as expected! Great! I just have the issue about "mine" tab not showing, which I want to force back, but definitely is a minor thing... – vguerrero Jul 24 '14 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.