Hide product category or product on shop page

Hide product category or product on shop page

  • August 2, 2022

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 !!!

Related Post

bb-theme-wordpress

BB Theme WordPress: A Comprehensive Review and Guide

Welcome to the world of BB Theme WordPress Are you searching for the perfect WordPress theme to bring your website to life? Look no further! BB WordPress Theme is here to help you design a […]

How-To-Delete-A-WordPress

How to Delete a Theme in WordPress: A Step-by-Step Guide

Easy Ways to Delete Any Theme in WordPress Do you want to remove a WordPress theme and are looking for guidance on how to do it? Removing a theme can help declutter your site and […]

Salient-WP

Salient WordPress Theme: A Comprehensive Guide for Beginners

If you’re looking for a WordPress theme that combines versatility, functionality, and style, the salient wordpress theme is worth considering. salient wordpress theme is a multi-purpose WordPress theme that caters to various niches, including business, […]