Continuing the discussion from Hide Category based on user role/membership plan ID:
Hi I noticed this code snippet which i have added for hiding specific categories in the listing submission form from specific user roles, however i would also like to hide viewing specific categories from specific user roles so that specific users can’t browse listings from specific categories.
In my case these hidden categories would be the ones that are visible to them in the add a listing submission page.
I have created 2 user roles (“teachers” & “students”) and 2 categories “listings from teachers” and “listings from students” (one category for each user role)
(both categories are organised further by including 3 sub categories each but im not sure if thats relevant to mention here.)
I have used the current snippet you have shared to just show the category the user should post their listing on and hide the other category, this way “teachers” only see “listings from teachers” to add their listing to and “students” only see “listings from students” to add their listing to. Thanks for that snippet btw!
So now ive hidden the corresponding categories in the add listing submission page i would like to do the same to the browse listings section whereby “teachers” only see the “listings from students” category and “students” only see the “collaborations from teachers” category, i would me eternally grateful if the snippet could be adjusted to provide this function. Thanks for reading.
Please try using the template_redirect hook for this purpose, you can check if it’s the listing category page via is_tax('hp_listing_category') condition, check the current membership plan (this part can be copied from the linked snippet and possibly adjusted), and redirect users to a custom page via wp_safe_redirect (e.g. to the Plans page). Note that this will not exclude searching by this categories via the filters form, this would require further customizations.
Thanks for your help, this is what chat gpt has made me with the help of your response so far: (bear in mind the category numbers are completely wrong here but i have put the right numbers for my own test) the problem is it still does not hide “all categories” tab on filter, any ideas? // 1. Restrict categories in the listing submission form
add_filter(
'hivepress/v1/models/listing/fields',
function($fields){
if(!is_user_logged_in()){
return $fields;
}
// Get user.
$user = get_userdata(get_current_user_id());
if(in_array('teachers', $user->roles)){
$disabled_categories_ids = [2]; // Assuming 2 is the ID for 'listings from students' category
} elseif (in_array('students', $user->roles)){
$disabled_categories_ids = [1]; // Assuming 1 is the ID for 'listings from teachers' category
}
if(isset($fields['categories']['options'])){
foreach($disabled_categories_ids as $category_id){
unset($fields['categories']['options'][$category_id]);
}
}
return $fields;
},
10000
);
// 2. Redirect users trying to access restricted categories
add_action('template_redirect', function(){
if(!is_user_logged_in()){
return;
}
// Check if on a category archive page.
if(is_tax('hp_listing_category')){
$user = wp_get_current_user();
// Define category restrictions for each role.
$teacher_restricted_categories = [2]; // 'listings from students'
$student_restricted_categories = [1]; // 'listings from teachers'
// Get the current category ID.
$queried_object = get_queried_object();
$current_category_id = $queried_object->term_id;
if(in_array('teachers', $user->roles) && in_array($current_category_id, $teacher_restricted_categories)){
wp_safe_redirect(home_url('/no-access')); // Redirect to a custom "No Access" page
exit;
} elseif(in_array('students', $user->roles) && in_array($current_category_id, $student_restricted_categories)){
wp_safe_redirect(home_url('/no-access')); // Redirect to a custom "No Access" page
exit;
}
}
});
// 3. Filter search results to exclude restricted categories
The code snippets seem to be ok, but they require testing in detail, please try testing and debugging them if you’re familiar with coding.
Please note that these code snippets also remove categories from selection (which can cause issues because listings may lose categories even if the listing vendor has membership, but the user who views listings hasn’t). If you just want to hide categories from viewing by users (category pages, also search results), I still recommend using a single callback function for the template_redirect hook.