neilgee
7/24/2017 - 8:21 AM

Hide WooCommerce Product Categories

Hide WooCommerce Product Categories

<?php   //<~ dont add me into functions.php 

add_action( 'woocommerce_product_query', 'prefix_custom_pre_get_posts_query' );
/**
 * Hide Product Cateories from targetted pages in WooCommerce
 * @link https://gist.github.com/stuartduff/bd149e81d80291a16d4d3968e68eb9f8#file-wc-exclude-product-category-from-shop-page-php
 *
 */
function prefix_custom_pre_get_posts_query( $q ) {
	
	if( is_shop() || is_page('awards') ) { // set conditions here

	    $tax_query = (array) $q->get( 'tax_query' );
	
	    $tax_query[] = array(
	           'taxonomy' => 'product_cat',
	           'field'    => 'slug',
	           'terms'    => array( 'cat1', 'cat2' ), // set product categories here
	           'operator' => 'NOT IN'
	    );
	
	
	    $q->set( 'tax_query', $tax_query );
	}
}