AND {$this->lookup_table_name}.taxonomy='{$encoded_taxonomy}' AND {$this->lookup_table_name}.term_id IN $term_ids_sql {$in_stock_clause}"; if ( ! empty( $term_ids ) ) { $attributes_to_filter_by = \WC_Query::get_layered_nav_chosen_attributes(); if ( ! empty( $attributes_to_filter_by ) ) { $and_term_ids = array(); foreach ( $attributes_to_filter_by as $taxonomy => $data ) { if ( 'and' !== $data['query_type'] ) { continue; } $all_terms = get_terms( $taxonomy, array( 'hide_empty' => false ) ); $term_ids_by_slug = wp_list_pluck( $all_terms, 'term_id', 'slug' ); $term_ids_to_filter_by = array_values( array_intersect_key( $term_ids_by_slug, array_flip( $data['terms'] ) ) ); $and_term_ids = array_merge( $and_term_ids, $term_ids_to_filter_by ); } if ( ! empty( $and_term_ids ) ) { $terms_count = count( $and_term_ids ); $term_ids_list = '(' . join( ',', $and_term_ids ) . ')'; // The extra derived table ("SELECT product_or_parent_id FROM") is needed for performance // (causes the filtering subquery to be executed only once). $query['where'] .= " AND product_or_parent_id IN ( SELECT product_or_parent_id FROM ( SELECT product_or_parent_id FROM {$this->lookup_table_name} lt WHERE is_variation_attribute=0 {$in_stock_clause} AND term_id in {$term_ids_list} GROUP BY product_id HAVING COUNT(product_id)={$terms_count} UNION SELECT product_or_parent_id FROM {$this->lookup_table_name} lt WHERE is_variation_attribute=1 {$in_stock_clause} AND term_id in {$term_ids_list} ) temp )"; } } else { $query['where'] .= $in_stock_clause; } } elseif ( $hide_out_of_stock ) { $query['where'] .= " AND {$this->lookup_table_name}.in_stock=1"; } $search_query_sql = \WC_Query::get_main_search_query_sql(); if ( $search_query_sql ) { $query['where'] .= ' AND ' . $search_query_sql; } $query['group_by'] = 'GROUP BY terms.term_id'; $query['group_by'] = "GROUP BY {$this->lookup_table_name}.term_id"; return $query; } /** * Get the query for counting products by terms NOT using the product attributes lookup table. * * @param \WP_Tax_Query $tax_query The current main tax query. * @param \WP_Meta_Query $meta_query The current main meta query. * @param string $term_ids The term ids to include in the search. * @return array An array of SQL query parts. */ private function get_product_counts_query_not_using_lookup_table( $tax_query, $meta_query, $term_ids ) { global $wpdb; $meta_query_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' ); $tax_query_sql = $tax_query->get_sql( $wpdb->posts, 'ID' ); // Generate query. $query = array(); $query['select'] = "SELECT COUNT( DISTINCT {$wpdb->posts}.ID ) AS term_count, terms.term_id AS term_count_id"; $query['from'] = "FROM {$wpdb->posts}"; $query['join'] = " INNER JOIN {$wpdb->term_relationships} AS term_relationships ON {$wpdb->posts}.ID = term_relationships.object_id INNER JOIN {$wpdb->term_taxonomy} AS term_taxonomy USING( term_taxonomy_id ) INNER JOIN {$wpdb->terms} AS terms USING( term_id ) " . $tax_query_sql['join'] . $meta_query_sql['join']; $term_ids_sql = $this->get_term_ids_sql( $term_ids ); $query['where'] = " WHERE {$wpdb->posts}.post_type IN ( 'product' ) AND {$wpdb->posts}.post_status = 'publish' {$tax_query_sql['where']} {$meta_query_sql['where']} AND terms.term_id IN $term_ids_sql"; $search_query_sql = \WC_Query::get_main_search_query_sql(); if ( $search_query_sql ) { $query['where'] .= ' AND ' . $search_query_sql; } $query['group_by'] = 'GROUP BY terms.term_id'; return $query; } /** * Formats a list of term ids as "(id,id,id)". * * @param array $term_ids The list of terms to format. * @return string The formatted list. */ private function get_term_ids_sql( $term_ids ) { return '(' . implode( ',', array_map( 'absint', $term_ids ) ) . ')'; } }