5

For one of my custom post types I want a specific user to be able to edit existing posts created by admin but not be able to Add New posts.

How can this be done?

If I define the user role as not being able to Publish it still allows them to add a new post and submit for review.

2 Answers 2

3

You will have to do something like this:

function hide_buttons() {
    global $current_screen;

    if($current_screen->id == 'edit-post' && !current_user_can('publish_posts')) {
        echo '<style>.add-new-h2{display: none;}</style>';  
    }
}
add_action('admin_head','hide_buttons');

See: http://erisds.co.uk/wordpress/spotlight-wordpress-admin-menu-remove-add-new-pages-or-posts-link for reference

1
  • 1
    One caveat here: The user will still be able to add a new post if they go to the page directly, this just removes the button, not the user's capability to add new posts.
    – ezekielDFM
    Feb 10, 2014 at 14:12
6

The previous answer only hides the menu item with CSS, and as @ezejielDFM points out, it won't stop users from actually being able to add posts.

Instead, when registering your custom post type, you need to set the create_posts value to do_not_allow (or false in Wordpress versions below 4.5) and crucially set the map_meta_cap to true.

register_post_type( 'custom_post_type_name', array(
    'capability_type' => 'post',
        'capabilities' => array(
        'create_posts' => 'do_not_allow', // Prior to Wordpress 4.5, this was false
    ),
    'map_meta_cap' => true, //  With this set to true, users will still be able to edit & delete posts
));

If map_meta_cap is left out, it defaults to false and although you've disabled the ability to Add New posts, you also won't be able to edit or delete existing ones either, so make sure to include that value.

Full credit goes to this answer on Stack Overflow.

1
  • 1
    the problem with this approach is, that as soon as I include 'create_posts' in the capabilities array, I can't display the list of my custom post type, so I can't even edit them ... Nov 25, 2021 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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