Created
February 10, 2020 06:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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