public static function get_main_tax_query() { global $wp_the_query; $tax_query = isset($wp_the_query->tax_query, $wp_the_query->tax_query->queries) ? $wp_the_query->tax_query->queries : array(); return $tax_query; }
/** * Count products after other filters have occured by adjusting the main query. * @param int $rating * @return int */ protected function get_filtered_product_count($rating) { global $wpdb; $tax_query = WC_Query::get_main_tax_query(); $meta_query = WC_Query::get_main_meta_query(); // Unset current rating filter foreach ($meta_query as $key => $query) { if (!empty($query['rating_filter'])) { unset($meta_query[$key]); } } // Set new rating filter $meta_query[] = array('key' => '_wc_average_rating', 'value' => $rating, 'compare' => '>=', 'type' => 'DECIMAL', 'rating_filter' => true); $meta_query = new WP_Meta_Query($meta_query); $tax_query = new WP_Tax_Query($tax_query); $meta_query_sql = $meta_query->get_sql('post', $wpdb->posts, 'ID'); $tax_query_sql = $tax_query->get_sql($wpdb->posts, 'ID'); $sql = "SELECT COUNT( {$wpdb->posts}.ID ) FROM {$wpdb->posts} "; $sql .= $tax_query_sql['join'] . $meta_query_sql['join']; $sql .= " WHERE {$wpdb->posts}.post_type = 'product' AND {$wpdb->posts}.post_status = 'publish' "; $sql .= $tax_query_sql['where'] . $meta_query_sql['where']; return absint($wpdb->get_var($sql)); }