skip to Main Content

I’m looking to exclude a Category from the Shop home page of my woocommerce store.

The loop I am using is the default loop internal to the plugin. I am not using a shortcode to call in the loop, nor am I using a custom php query.

The Shop home page is set to display every category. From there the user clicks through the various child categories until the appropriate products are revealed.

I have crawled through every conceivable solution within this website, none of which have has any effect – I’m hoping someone can help.

I want the exclude the following category:

Category Name: Z_PRODUCT CATALOGUE

ID: 122

Slug: product-catalogue

Examples of code I have attempted to use but failed:

function se_customize_product_shortcode( $args, $atts ) {
    if ( is_page( 'products' ) ) {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => array( 'product-catalogue' ),
                'operator' => 'NOT IN'
            )
       );
    }

    return $args;
}
add_filter( 'woocommerce_shortcode_products_query', 'se_customize_product_shortcode', 10, 2 );

and

add_filter( 'pre_get_posts', 'custom_query_exclude_products', 900, 1 );
function custom_query_exclude_products( $query ) {
    if ( is_admin() || is_search() || is_shop() ) return $query;


        $query->set( 'tax_query', array( array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => array( 'product-catalogue' ),
            'operator' => 'NOT IN'
        ) ) );


    remove_action( 'pre_get_posts', 'custom_query_exclude_products' );

    return $query;
}

2

Answers


  1. Chosen as BEST ANSWER

    Unfortunatly this did not work for me. I have however found a soltuion which worked.

    add_filter( 'get_terms', 'sct_hide_cat', 10, 3 );
    function sct_hide_cat( $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( 'product-catalogue' ) ) ) {
                    $new_terms[] = $term;
                }
            }
            $terms = $new_terms;
        }
        return $terms;
    }
    

  2. This snippet works for me. It’s based on this woocommerce support article. You need to put the slug for your excluded category in the terms array; double check that you have the right slug.

    /**
     * Exclude out-of-print products from the shop / search pages
     * "tax" here means "taxonomy" (a WordPress thing for handling product and other categories).
     * It doesn't mean "sales tax".
     */
    function mystore_pre_get_posts_query( $q ) {
        $tax_query = (array) $q->get( 'tax_query' );
        $tax_query[] = array(
               'taxonomy' => 'product_cat',
               'field' => 'slug',
               'terms' => array( 'out-of-print' ), // Don't display products in the out-of-print category on the shop page.
               'operator' => 'NOT IN'
        );
        $q->set( 'tax_query', $tax_query );
    }
    add_action( 'woocommerce_product_query', 'mystore_pre_get_posts_query' );  
    

    I use the woocommerce_product_query action hook, but you try to use the woocommerce_shortcode_products_query filter hook.

    It generally takes about six years at the WordPress equivalent of the Hogwarts Academy of Magic to learn what each arcane hook does.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search