From dde1e3ce841da0f2d3848b2ee8f72097a5b3bf61 Mon Sep 17 00:00:00 2001 From: lucasstark Date: Wed, 21 Mar 2012 08:55:34 -0300 Subject: [PATCH] Efficiency update for price filter --- widgets/widget-price_filter.php | 76 +++++++++++++++------------------ 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/widgets/widget-price_filter.php b/widgets/widget-price_filter.php index a6881b2dbba..5fe336cf9d9 100644 --- a/widgets/widget-price_filter.php +++ b/widgets/widget-price_filter.php @@ -34,49 +34,43 @@ function woocommerce_price_filter_init() { /** * Price Filter post filter */ -function woocommerce_price_filter( $filtered_posts ) { +function woocommerce_price_filter($filtered_posts) { + global $wpdb; + + if (isset($_GET['max_price']) && isset($_GET['min_price'])) : - if (isset($_GET['max_price']) && isset($_GET['min_price'])) : - - $matched_products = array(); - - $matched_products_query = get_posts(array( - 'post_type' => array( - 'product_variation', - 'product' - ), - 'post_status' => 'publish', - 'posts_per_page' => -1, - 'meta_query' => array( - array( - 'key' => '_price', - 'value' => array( $_GET['min_price'], $_GET['max_price'] ), - 'type' => 'NUMERIC', - 'compare' => 'BETWEEN' - ) - ) - )); + $matched_products = array(); + $low = floatval($_GET['min_price']); + $high = floatval($_GET['max_price']); + + $matched_products_query = $wpdb->get_results( + $wpdb->prepare( + "SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts + INNER JOIN $wpdb->postmeta ON ID = post_id + WHERE (post_type='product' OR post_type='product_variation') AND post_status = 'publish' AND meta_key = %s AND meta_value BETWEEN %d AND %d", '_price', $low, $high), OBJECT_K); - if ($matched_products_query) : - foreach ($matched_products_query as $product) : - if ($product->post_type == 'product') $matched_products[] = $product->ID; - if ($product->post_parent>0 && !in_array($product->post_parent, $matched_products)) - $matched_products[] = $product->post_parent; - endforeach; - endif; - - // Filter the id's - if (sizeof($filtered_posts)==0) : - $filtered_posts = $matched_products; - $filtered_posts[] = 0; - else : - $filtered_posts = array_intersect($filtered_posts, $matched_products); - $filtered_posts[] = 0; - endif; - - endif; - - return (array) $filtered_posts; + + if ($matched_products_query) : + foreach ($matched_products_query as $product) : + if ($product->post_type == 'product') + $matched_products[] = $product->ID; + if ($product->post_parent > 0 && !in_array($product->post_parent, $matched_products)) + $matched_products[] = $product->post_parent; + endforeach; + endif; + + // Filter the id's + if (sizeof($filtered_posts) == 0) : + $filtered_posts = $matched_products; + $filtered_posts[] = 0; + else : + $filtered_posts = array_intersect($filtered_posts, $matched_products); + $filtered_posts[] = 0; + endif; + + endif; + + return (array) $filtered_posts; } /**