# Hide specifically viewing Category based on user role/membership plan ID

**URL:** <https://community.hivepress.io/t/hide-specifically-viewing-category-based-on-user-role-membership-plan-id/12011>\
**Category:** HivePress\
**Tags:** code\
**Created:** [September 2, 2024, 6:18pm UTC](https://community.hivepress.io/t/hide-specifically-viewing-category-based-on-user-role-membership-plan-id/12011 "2024-09-02T18:18:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![angus](https://community.hivepress.io/letter_avatar_proxy/v4/letter/a/b3f665/32.png) [@angus](https://community.hivepress.io/u/angus)\
**Post date:** [September 2, 2024, 6:18pm UTC](https://community.hivepress.io/t/hide-specifically-viewing-category-based-on-user-role-membership-plan-id/12011/1 "2024-09-02T18:18:41Z")

</div>

Continuing the discussion from [Hide Category based on user role/membership plan ID](https://community.hivepress.io/t/hide-category-based-on-user-role-membership-plan-id/4495):  
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.

---

<div class="post-metadata">

**Author:** ![ihor](https://community.hivepress.io/user_avatar/community.hivepress.io/ihor/32/5_2.png) [@ihor](https://community.hivepress.io/u/ihor)\
**Post date:** [September 3, 2024, 11:08pm UTC](https://community.hivepress.io/t/hide-specifically-viewing-category-based-on-user-role-membership-plan-id/12011/3 "2024-09-03T23:08:56Z")

</div>

Hi,

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.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![angus](https://community.hivepress.io/letter_avatar_proxy/v4/letter/a/b3f665/32.png) [@angus](https://community.hivepress.io/u/angus)\
**Post date:** [September 4, 2024, 9:23am UTC](https://community.hivepress.io/t/hide-specifically-viewing-category-based-on-user-role-membership-plan-id/12011/4 "2024-09-04T09:23:17Z")

</div>

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

```auto
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

```auto
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

```auto
add_action('pre_get_posts', function($query){
    if(!is_admin() && $query->is_main_query() && $query->is_search){
        $user = wp_get_current_user();

        if(in_array('teachers', $user->roles)){
            $query->set('tax_query', array(
                array(
                    'taxonomy' => 'hp_listing_category',
                    'field' => 'id',
                    'terms' => [2], // Exclude 'listings from students'
                    'operator' => 'NOT IN'
                )
            ));
        } elseif(in_array('students', $user->roles)){
            $query->set('tax_query', array(
                array(
                    'taxonomy' => 'hp_listing_category',
                    'field' => 'id',
                    'terms' => [1], // Exclude 'listings from teachers'
                    'operator' => 'NOT IN'
                )
            ));
        }
    }
});

```

---

<div class="post-metadata">

**Author:** ![ihor](https://community.hivepress.io/user_avatar/community.hivepress.io/ihor/32/5_2.png) [@ihor](https://community.hivepress.io/u/ihor)\
**Post date:** [September 6, 2024, 7:30pm UTC](https://community.hivepress.io/t/hide-specifically-viewing-category-based-on-user-role-membership-plan-id/12011/6 "2024-09-06T19:30:53Z")

</div>

Sorry for the delay.

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.

---

<div class="post-metadata">

**Author:** ![system](https://community.hivepress.io/user_avatar/community.hivepress.io/system/32/13990_2.png) [@system](https://community.hivepress.io/u/system)\
**Post date:** [October 6, 2024, 7:31pm UTC](https://community.hivepress.io/t/hide-specifically-viewing-category-based-on-user-role-membership-plan-id/12011/7 "2024-10-06T19:31:01Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
