Skip to content

Instantly share code, notes, and snippets.

@premitheme
Forked from rynaldos-zz/wc-hide-cats-archive-page.php
Last active March 25, 2018 16:23
Show Gist options
  • Save premitheme/b6e760b6e12103fd507a52371713e238 to your computer and use it in GitHub Desktop.
Save premitheme/b6e760b6e12103fd507a52371713e238 to your computer and use it in GitHub Desktop.
Exclude WooCommerce categories from shop and other pages

Exclude WooCommerce categories

For Shop & Pages

Replace woo with the product category slug of the category you need to exclude. Multiple categories can be excluded like this array( 'woo1', 'woo2', 'woo3' ).

To exclude from another page, replace is_shop() with is_page( 'YOUR_PAGE_SLUG' ) and enter your page slug in place of YOUR_PAGE_SLUG.

For Woo Categories Widget

Enter the id of the category you want to exclude in place of 30. Multiple categories can be excluded like this array( '30', '31', '32' ).

add_filter( 'get_terms', 'premitheme_exclude_woo_cats_page', 10, 3 );
function premitheme_exclude_woo_cats_page( $terms, $taxonomies, $args ) {
$new_terms = array();
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( 'woo' ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
add_filter( 'woocommerce_product_categories_widget_args', 'premitheme_exclude_woo_cats_widget' );
function premitheme_exclude_woo_cats_widget( $args ) {
$args['exclude'] = array('30' );
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment