PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 17, 2022

[FIXED] Exclude categories from product_categories shortcode using slug

 January 17, 2022     woocommerce, wordpress     No comments   

Issue

<div class="categories">[product_categories limit="10" columns="5" orderby="menu_order"]</div>

Is it possible to hook into product categories shortcode and filter out specific categories from returning (by slug), like you can with products using woocommerce_shortcode_products_query hook.

add_filter( 'woocommerce_shortcode_products_query', 'xaa_remove_category', 50, 3);
function xaa_remove_category( $query_args, $atts, $loop_name ){
    if( is_front_page() ){
        
        $query_args['tax_query'] = array(array( 
        'taxonomy' => 'product_cat', 
        'field' => 'slug', 
        'terms' => ['custom-category-slug'],
        'operator' => 'NOT IN'
        ));
        
    }
    return $query_args;
}

Like this but for product categories


Solution

You can woocommerce_product_categories filter hook. try the below code.

add_filter( 'woocommerce_product_categories', 'exclude_product_category', 10, 1 );
function exclude_product_category( $product_categories ){

    $exclude_category = 'test-2'; // your category slug

    foreach ( $product_categories as $key => $product_cat ) {
        if( $product_cat->slug == $exclude_category ){
            unset( $product_categories[$key] );
        }
    }
    
    return $product_categories;

}


Answered By - Bhautik
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing