Hide product category or product on shop page
Hello Guys today we gonna learn how can we hide specific product category, specific product on shop page.
We are going to do this with the help of code snippets. You can directly add these code snippets in your child theme’s functions.php file or with the help of code snippet plugin.
1. Hide product category from shop page
add category slug with comma separated for hiding in shop page
/** * @snippet hide category in shop page, archive page * @author Deep Prakash Goyal * @website https://wpexpertdeep.com/ */ add_filter( 'get_terms', 'wpexpertdeep_get_subcategory_terms', 10, 3 ); function wpexpertdeep_get_subcategory_terms( $terms, $taxonomies, $args ) { $new_terms = array(); // if it is a product category and on the shop page if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) { foreach( $terms as $key => $term ) { //pass the slug name here if ( !in_array( $term->slug, array( 'hidden-product-cat', 'uncategorized' ) ) ) { $new_terms[] = $term; } } $terms = $new_terms; } return $terms; }
2. Hide specific category product in shop and archive page
add category slug with comma separated for hiding the products
/** * @snippet hide specific category product in shop page, archive page * @author Deep Prakash Goyal * @website https://wpexpertdeep.com/ */ add_action( 'woocommerce_product_query', 'wpexpertdeep_custom_pre_get_posts_query' ); function wpexpertdeep_custom_pre_get_posts_query( $q ) { $tax_query = (array) $q->get( 'tax_query' ); $tax_query[] = array( 'taxonomy' => 'product_cat', 'field' => 'slug', // Don't display products in the clothing category on the shop page. 'terms' =>array( 'hidden-product-cat', 'uncategorized'), 'operator' => 'NOT IN' ); $q->set( 'tax_query', $tax_query ); }
With the help of these 2 methods you can hide category or product in shop page.
Add these snippets in your child theme’s functions.php file
Happy Coding !!!