对于我的自定义帖子类型之一,我希望特定用户能够编辑管理员创建的现有帖子,但不能添加新帖子。

如何才能做到这一点?

如果我将用户角色定义为无法发布的角色,仍然可以允许他们添加新帖子并提交审查。

有帮助吗?

解决方案

您将必须做这样的事情:

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');

看: http://erisds.co.uk/wordpress/spotlight-wordpress-admin-menu-remove-add-new-pages-orpoges-orposts-link 以供参考

其他提示

上一个答案仅隐藏了CSS的菜单项,正如@ezejieldfm指出的那样,它不会阻止用户的 实际上 能够添加帖子。

相反,在注册自定义帖子类型时,您需要设置 create_posts 价值为 do_not_allow (或者 false 在WordPress版本以下4.5中),并至关重要 map_meta_captrue.

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
));

如果 map_meta_cap 被排除在外,默认为 false 尽管您已经削弱了能力 添新 帖子,您也无法编辑或删除现有的帖子,因此请确保包含该值。

全部功劳是 这个答案 在堆栈溢出上。

许可以下: CC-BY-SA归因
scroll top