Remove woocommerce hidden products from wordpress default search result

I need to remove woocommerce hidden products from default wordpress search result, hidden products are only hidden when doing a woocommerce search. Tnx in advance.

Related posts

Leave a Reply

2 comments

  1. Alter the search loop and use the following codes below

    $_pf = new WC_Product_Factory();
    $_product = $_pf->get_product($id); // assuming $id is available as the code is inside the loop
    $_product->is_visible()
    
  2. In Woocommerce 3+ this changed from metadata to a taxonomy

    add_action( 'pre_get_posts', 'search_query_fix' );
    function search_query_fix( $query = false ) {
      if(!is_admin() && is_search()){
        $query->set( 'tax_query', array(
            array(
                'taxonomy' => 'product_visibility',
                'field'    => 'name',
                'terms'    => 'exclude-from-search',
                'operator' => 'NOT IN',
            )
        ));
      }
    }