我有两个角色,
两者都会在自定义帖子类型中发布" Companies
"。
当他们即将编辑/删除自己的帖子时,我不希望他们看到其他角色发布的其他帖子。目前它正在显示后端其他角色职位的标题。角色无法在后端编辑/删除其他角色,但我看到其他角色按视图链接发布标题。
如何摆脱它?
答案 0 :(得分:0)
您可以使用
parse_query
过滤器来实现此目的$pagenow
全局变量。
author__in
密钥传递这些ID。这是代码
add_filter('parse_query', 'wh_hideOthersRolePost');
function wh_hideOthersRolePost($query) {
global $pagenow;
global $current_user;
$my_custom_post_type = 'companies'; // <-- replace it with your post_type slug
$my_custom_role = ['members', 'recruiter']; // <-- replace it with your role slug
//if user is not logged in or the logged in user is admin then dont do anything
if (!is_user_logged_in() && !is_admin())
return;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if(!in_array($user_role, $my_custom_role))
return;
$user_args = [
'role' => $user_role,
'fields ' => 'ID'
];
//getting all the user_id with the specific role.
$users = get_users($user_args);
//print_r($users);
if (!count($users)) {
return;
}
$author__in = []; // <- variable to store all User ID with specific role
foreach ($users as $user) {
$author__in[] = $user->ID;
}
if (is_admin() && $pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] == $my_custom_post_type){
//retriving post from specific authors which has the above mentioned role.
$query->query_vars['author__in'] = $author__in;
}
}
代码进入活动子主题(或主题)的function.php文件。或者也可以在任何插件php文件中。
代码已经过测试并且有效。
希望这有帮助!
一些相关问题:Hide "free" orders in WooCommerce orders section from admin panel