Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save apsolut/1c10bf2b8ad98ee45d947763318086c9 to your computer and use it in GitHub Desktop.
Save apsolut/1c10bf2b8ad98ee45d947763318086c9 to your computer and use it in GitHub Desktop.
giftable-exclude-category-from-shop-archive
<?php //<- dont add me into functions.php
/**
* Hide Gift Product Category from Shop
* https://wordpress.org/support/topic/hide-gift-product-category-from-shop/
* Create category Exclude or Gifts
* Category slug: exclude, gifts
* add gifts that you want to exclude from shop archive to this category
*/
add_action( 'woocommerce_product_query', 'giftable_custom_pre_get_posts_query' );
/**
* Hide Product Categories from targeted pages in WooCommerce
* @link https://gist.github.com/stuartduff/bd149e81d80291a16d4d3968e68eb9f8#file-wc-exclude-product-category-from-shop-page-php
*
*/
function giftable_custom_pre_get_posts_query( $exclude_query ) {
if( is_shop() || is_page('or-some-special-page') ) { // set conditions here
$tax_query = (array) $exclude_query->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'exclude', 'gifts' ), // set product categories here
'operator' => 'NOT IN'
);
$exclude_query->set( 'tax_query', $tax_query );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment