最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - Hide a product with a certain tag from product-category

programmeradmin5浏览0评论

I am trying to make products with a certain tag disappear completely from the store, including when customers are browsing categories.

In another question I found this function that help me hide products on the shop page, which is great.

    function exclude_specific_tag( $q ) {
    if (is_shop()){
        $tax_query = (array) $q->get( 'tax_query' );
        $tax_query[] = array(
            'taxonomy' => 'product_tag',
            'field' => 'slug',
            'terms' => array( 'special' ), // tag name to hide ''
            'operator' => 'NOT IN'
        );
        $q->set( 'tax_query', $tax_query );
    }
}
add_action( 'woocommerce_product_query', 'exclude_specific_tag' );

Now I need to hide that certain tag from product-categories. The goal is to hide these products with the tag completely from the store, unless I show them in a specific page or post.

Any help and answers are greatly appreciated.

I am trying to make products with a certain tag disappear completely from the store, including when customers are browsing categories.

In another question I found this function that help me hide products on the shop page, which is great.

    function exclude_specific_tag( $q ) {
    if (is_shop()){
        $tax_query = (array) $q->get( 'tax_query' );
        $tax_query[] = array(
            'taxonomy' => 'product_tag',
            'field' => 'slug',
            'terms' => array( 'special' ), // tag name to hide ''
            'operator' => 'NOT IN'
        );
        $q->set( 'tax_query', $tax_query );
    }
}
add_action( 'woocommerce_product_query', 'exclude_specific_tag' );

Now I need to hide that certain tag from product-categories. The goal is to hide these products with the tag completely from the store, unless I show them in a specific page or post.

Any help and answers are greatly appreciated.

Share Improve this question asked Sep 10, 2018 at 10:32 delta150delta150 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You should be able to add to your condition:

if (is_shop() || is_product_category()) {

This will exclude products with the tag from both the shop page and all product categories. You may also want to add || is_product_tag() to make sure they don't show up on tag archives. WooCommerce's conditional tags reference can help you find all the conditions you may be interested in using.

发布评论

评论列表(0)

  1. 暂无评论