WC_Query::get_tax_query PHP Method

get_tax_query() public method

Appends tax queries to an array.
public get_tax_query ( array $tax_query = [] ) : array
$tax_query array
return array
    public function get_tax_query($tax_query = array())
    {
        if (!is_array($tax_query)) {
            $tax_query = array();
        }
        // Layered nav filters on terms
        if ($_chosen_attributes = $this->get_layered_nav_chosen_attributes()) {
            foreach ($_chosen_attributes as $taxonomy => $data) {
                $tax_query[] = array('taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $data['terms'], 'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN', 'include_children' => false);
            }
        }
        return array_filter(apply_filters('woocommerce_product_query_tax_query', $tax_query, $this));
    }

Usage Example

 /**
  * Piggyback WooCommerce's Layered Navigation and inject SearchWP results where applicable
  *
  * @param $filtered_posts
  *
  * @return array
  */
 function post_in($filtered_posts)
 {
     global $wp_query;
     // WooCommerce 2.6 introduced tax/meta query piggybacking that's much better
     if (function_exists('WC') && !empty(WC()->version) && version_compare(WC()->version, '2.6', '<')) {
         return $this->legacy_post_in($filtered_posts);
     }
     if ($this->is_woocommerce_search() && ($query = get_search_query())) {
         if (!empty($this->results)) {
             return $this->results;
         }
         $searchwp_engine = 'default';
         $swppg = get_query_var('paged') ? get_query_var('paged') : 1;
         // force SearchWP to only consider the filtered posts
         if (!empty($filtered_posts)) {
             $this->filtered_posts = $filtered_posts;
             add_filter('searchwp_include', array($this, 'include_filtered_posts'));
         }
         do_action('searchwp_woocommerce_before_search', $this);
         // don't log this search, it's redundant
         add_filter('searchwp_log_search', '__return_false');
         $wc_query = new WC_Query();
         $args = array('s' => $query, 'engine' => $searchwp_engine, 'page' => $swppg, 'fields' => 'ids', 'posts_per_page' => -1, 'tax_query' => $wc_query->get_tax_query(), 'meta_query' => $wc_query->get_meta_query());
         $args = apply_filters('searchwp_woocommerce_query_args', $args);
         $results = new SWP_Query($args);
         $this->results = $results->posts;
         remove_filter('searchwp_log_search', '__return_false');
         return $this->results;
     } elseif (!empty($this->results)) {
         return $this->results;
     }
     return (array) $filtered_posts;
 }