How to display only specific categories on shop page on WooCommerce

I have been looking for this for a while with no luck. Finally I found some code that with small alterations worked. There can be other more effective solutions for this out there, but I am not aware of them.

So here it is.

Let’s suppose that we only want to display 2 categories with slugs “pc-games” and “sports-games” on our default Woocommerce shop page. We just need to paste the following code on our theme’s functions.php file:

add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
 function get_subcategory_terms( $terms, $taxonomies, $args ) {
 $new_terms = array();

if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() ) {
 foreach ( $terms as $key => $term ) {

if ( in_array( $term->slug, array( 'sports-games','pc-games' ) ) ) {
 $new_terms[] = $term;
 }
 }
 $terms = $new_terms;
 }
 return $terms;
 }

If we wanted to do the opposite, hence hide/exlude the above categories from the default shop page, we would need the following (changes only the ! on line 8)

add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
 function get_subcategory_terms( $terms, $taxonomies, $args ) {
 $new_terms = array();

if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() ) {
 foreach ( $terms as $key => $term ) {

if ( !in_array( $term->slug, array( 'sports-games','pc-games' ) ) ) {
 $new_terms[] = $term;
 }
 }
 $terms = $new_terms;
 }
 return $terms;
 }

Hope that helps! Share this

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.