# Product Category Visible Only to Certain Roles

> Hides a category's products from the shop, search and product pages for everyone except the roles you choose, such as trade customers or members.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, catalog, roles, visibility
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-category-for-roles-only/

## Code

```php
$wpide_private_cats = array(
    'categories' => array( 'trade-only' ),         // Category slugs to hide.
    'roles'      => array( 'wholesale_customer' ), // Roles that can see them ('logged_in' = any logged-in user).
);

$wpide_can_see_private = function () use ( $wpide_private_cats ) {
    if ( current_user_can( 'manage_woocommerce' ) ) {
        return true;
    }

    if ( ! is_user_logged_in() ) {
        return false;
    }

    return in_array( 'logged_in', $wpide_private_cats['roles'], true ) || array_intersect( $wpide_private_cats['roles'], (array) wp_get_current_user()->roles );
};

$wpide_private_tax_query = array(
    'taxonomy' => 'product_cat',
    'field'    => 'slug',
    'terms'    => $wpide_private_cats['categories'],
    'operator' => 'NOT IN',
);

add_action( 'woocommerce_product_query', function ( $query ) use ( $wpide_can_see_private, $wpide_private_tax_query ) {
    if ( $wpide_can_see_private() ) {
        return;
    }

    $tax_query   = (array) $query->get( 'tax_query' );
    $tax_query[] = $wpide_private_tax_query;
    $query->set( 'tax_query', $tax_query );
} );

add_action( 'pre_get_posts', function ( $query ) use ( $wpide_can_see_private, $wpide_private_tax_query ) {
    if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() || $wpide_can_see_private() ) {
        return;
    }

    $tax_query   = (array) $query->get( 'tax_query' );
    $tax_query[] = $wpide_private_tax_query;
    $query->set( 'tax_query', $tax_query );
} );

add_filter( 'woocommerce_shortcode_products_query', function ( $args ) use ( $wpide_can_see_private, $wpide_private_tax_query ) {
    if ( ! $wpide_can_see_private() ) {
        $args['tax_query']   = isset( $args['tax_query'] ) ? (array) $args['tax_query'] : array();
        $args['tax_query'][] = $wpide_private_tax_query;
    }

    return $args;
} );

add_filter( 'woocommerce_related_products', function ( $ids ) use ( $wpide_can_see_private, $wpide_private_cats ) {
    if ( $wpide_can_see_private() ) {
        return $ids;
    }

    return array_values( array_filter( $ids, function ( $id ) use ( $wpide_private_cats ) {
        return ! has_term( $wpide_private_cats['categories'], 'product_cat', $id );
    } ) );
} );

add_action( 'template_redirect', function () use ( $wpide_can_see_private, $wpide_private_cats ) {
    $blocked = ( is_product() && has_term( $wpide_private_cats['categories'], 'product_cat', get_queried_object_id() ) )
        || ( is_product_category( $wpide_private_cats['categories'] ) );

    if ( $blocked && ! $wpide_can_see_private() ) {
        global $wp_query;
        $wp_query->set_404();
        status_header( 404 );
        nocache_headers();
    }
} );

add_filter( 'woocommerce_is_purchasable', function ( $purchasable, $product ) use ( $wpide_can_see_private, $wpide_private_cats ) {
    $id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();

    return $purchasable && ( $wpide_can_see_private() || ! has_term( $wpide_private_cats['categories'], 'product_cat', $id ) );
}, 10, 2 );
```

## Notes

Add 'logged_in' to the roles to show the category to any signed-in customer; direct links to hidden products show a 404 page.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Product Category Visible Only to Certain Roles" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
