0
我有兩個角色,WordPress的 - 隱藏其他角色的帖子在後臺
- 成員
- 招聘。
兩者都將在自定義帖子中輸入「Companies
」。
當他們即將編輯/刪除自己的帖子時,我不希望他們看到其他角色發佈的其他帖子。目前它正在後端顯示其他角色帖子的標題。角色無法編輯/刪除後端中的其他角色,但我通過視圖鏈接看到其他角色的帖子標題。
如何擺脫它?
我有兩個角色,WordPress的 - 隱藏其他角色的帖子在後臺
兩者都將在自定義帖子中輸入「Companies
」。
當他們即將編輯/刪除自己的帖子時,我不希望他們看到其他角色發佈的其他帖子。目前它正在後端顯示其他角色帖子的標題。角色無法編輯/刪除後端中的其他角色,但我通過視圖鏈接看到其他角色的帖子標題。
如何擺脫它?
您可以通過使用
parse_query
濾波器$pagenow
全局變量實現這一目標。
author__in
密鑰傳遞。下面是代碼
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