Skip to content

Instantly share code, notes, and snippets.

@kartikparmar
Created February 10, 2020 06:38
Show Gist options
  • Select an option

  • Save kartikparmar/6eb642757d9e8764e1ff94202546a8be to your computer and use it in GitHub Desktop.

Select an option

Save kartikparmar/6eb642757d9e8764e1ff94202546a8be to your computer and use it in GitHub Desktop.
Hide products of particular categories being displayed on the Shop page only for guest users
/**
* Exclude products from a particular category on the shop page
*/
function ts_pre_get_posts_query( $q ) {
if ( ! is_user_logged_in() ) { // Hide below mentioned categories for guest user
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'topwear' ), // Here add the slug of the categories need to be hide.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
}
add_action( 'woocommerce_product_query', 'ts_pre_get_posts_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment