允许查看帖子类型的编辑屏幕,但不允许进行/保存/发布/更新更改

时间:2015-09-22 作者:Satbir Kira

我一直在搜索有关如何做到这一点的文档。

有一个用户类型管理器,可以查看名为employees的自定义帖子类型的编辑页面。经理应该能够查看自定义帖子字段,但不能编辑它们。自定义职位类型员工没有自己的视图。如中所示,员工没有自己的页面。因此,这意味着只需授予查看权限,他们就会看到一个空白页。

Got A Working Solution:

custom_post_type_employee.php

add_action(\'init\',function() {
    //custom capability
    $capabilities = array(
        \'publish_posts\' => \'publish_employee\',
        \'edit_posts\' => \'edit_employees\',
        \'edit_others_posts\' => \'edit_others_employee\',
        \'delete_posts\' => \'delete_employees\',
        \'delete_others_posts\' => \'delete_others_employee\',
        \'read_private_posts\' => \'read_private_employee\',
        \'edit_post\' => \'edit_employee\',
        \'delete_post\' => \'delete_employee\',
        \'read_post\' => \'read_employee\'
    );

    /* Setting the options for post type */
    $args = array(
        ...
        \'capability_type\'       => \'employee\',
        \'capabilities\'          => $capabilities,
        ...
    );
    register_post_type(\'employee\', $args);
});

//add capabilities to admin
$role_admin = get_role(\'administrator\');
$admin_capabilities = array(
    \'publish_employee\' => true,
    \'edit_employees\' => true,
    \'edit_others_employee\' => true,
    \'delete_employees\' => true,
    \'delete_others_employee\' => true,
    \'read_private_employee\' => true,
    \'edit_employee\' => true,
    \'delete_employee\' => true,
    \'read_employee\' => true
);
foreach($admin_capabilities as $capability => $bool)
{
    $role_admin->add_cap( $capability , $bool );
}

//add capabilities to manager
$role_manager = get_role(\'db_manager\');
$manager_capabilities = array(
    \'publish_employee\' => false,
    \'edit_employees\' => true,
    \'edit_others_employee\' => false,
    \'delete_employees\' => false,
    \'delete_others_employee\' => false,
    \'read_private_employee\' => true,
    \'edit_employee\' => false,
    \'delete_employee\' => false,
    \'read_employee\' => true
);
foreach($manager_capabilities as $capability => $bool)
{
    $role_manager->add_cap( $capability , $bool );
}

single-employee.php

<?php 
get_header(); 

//if not logged in correctly exit
global $current_user;
$isAdmin =  in_array(\'administrator\', $current_user->roles);
$isManager =  in_array(\'db_manager\', $current_user->roles);
if (!is_user_logged_in() || !($isAdmin || $isManager )) 
{ 
    die("Must be logged in to access."); 
}


echo get_field("employee_title");
echo get_field("employee_email");

get_footer(); 
?>
特别注意:必须将edit\\u employees设置为true,经理才能看到列出创建的所有自定义帖子的页面。但是,当我们将edit\\u employee设置为false时,他们将无法编辑一个。

1 个回复
最合适的回答,由SO网友:Khaled Sadek 整理而成

使用创建页面模板WP_Query 仅显示所需字段managers 访问此页面这是基本想法,开始,如果发现任何问题,请询问