47
Hello WooCommerce Customizers!
Today we take a look at the WooCommerce Shop page and specifically at how to show only the category you want (and exclude all the others). Some store owners may need this, you never know the weird questions you get asked!
PHP Snippet: Only Show Products Belonging to One Category @ WooCommerce Shop Page
/**
* @snippet Display Products From 1 Category @ Shop Page
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire
* @compatible WooCommerce 6
* @donate $9 https://tutoraspire.com
*/
add_action( 'pre_get_posts', 'tutoraspire_only_one_category_woocommerce_shop' );
function tutoraspire_only_one_category_woocommerce_shop( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() && is_shop() ) {
$q->set( 'tax_query', array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'black' ), // change 'black' with your cat slug/s
'operator' => 'IN'
)));
}
}