Skip Navigation

[Resolved] Can I filter a wordpress archive using woocommerce product visibility setting?

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by Minesh 6 months, 4 weeks ago.

Assisted by: Minesh.

Author
Posts
#2701824

If I make a View listing WooCommerce products, the view filter settings include the ability to filter using Woo's built-in product visibility settings.

The same filter is not available when using a WordPress Archive to list Products. How can I achieve the same filtering result in the WordPress Archive?

Thanks, Simon

#2701970

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please confirm - are you using Toolset Block view or legacy version?

In addition to that - can you please tell me what where you added your archive and where you created the view that shows product visibility and with what product visibility you want to filter the view.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2701972

Hi. I'm talking about a legacy WordPress Archive, not a Block.

I want to _exclude_ Products where the visibility status is set to Hidden.

#2701974

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

To exclude hidden products from the product archive:

What if you try to add the following code to "Custom Code" section offered by Toolset:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

add_action( 'pre_get_posts', 'func_exclude_hidden_products_product_visibility', 999 );
function func_exclude_hidden_products_product_visibility( $query ) {
 
    if ( $query->is_main_query() && ! is_admin() && is_post_type_archive('product') ) {
 
         $tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'slug',
            'terms'    => array( 'exclude-from-catalog', 'exclude-from-search','hidden'),
            'operator' => 'NOT IN',
        );
 
        $query->set('tax_query', $tax_query);
 
    }
 
}
#2701988

That doesn't seem to work, I'm afraid, either added to Toolset Custom Code or directly to the theme's functions.php.

#2701989

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share problem URL and admin access details and also tell me few products that you set as hidden products.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2702001

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

The issue was you are on taxonomy archive product_cat so I've adjusted the code added to functions.php file as given under:

add_action( 'pre_get_posts', 'func_exclude_hidden_products_product_visibility', 999 );
    function func_exclude_hidden_products_product_visibility( $query ) {
  
    if ( $query->is_main_query() && ! is_admin() && $query->is_tax( 'product_cat' ) ) {
  
         $tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'slug',
            'terms'    => array( 'exclude-from-catalog', 'exclude-from-search'),
            'operator' => 'NOT IN',
        );
  
        $query->set('tax_query', $tax_query);
  
    }
  return $query;
}

Can you please confirm - it works as expected now.

#2702002

Great, that's done the job, thank you!