Prevent empty meta queries by filtering. Closes #2978.

This commit is contained in:
Mike Jolley 2013-04-17 16:29:43 +01:00
parent 8d2c2b5368
commit 77aa394093
13 changed files with 39 additions and 33 deletions

View File

@ -1083,6 +1083,7 @@ class WC_Product {
$meta_query = array();
$meta_query[] = $woocommerce->query->visibility_meta_query();
$meta_query[] = $woocommerce->query->stock_status_meta_query();
$meta_query = array_filter( $meta_query );
// Get the posts
$related_posts = get_posts( apply_filters('woocommerce_product_related_posts', array(

View File

@ -233,9 +233,7 @@ class WC_Query {
public function product_query( $q ) {
// Meta query
$meta_query = (array) $q->get( 'meta_query' );
$meta_query[] = $this->visibility_meta_query();
$meta_query[] = $this->stock_status_meta_query();
$meta_query = $this->get_meta_query( $q->get( 'meta_query' ) );
// Ordering
$ordering = $this->get_catalog_ordering_args();
@ -260,9 +258,11 @@ class WC_Query {
$q->set( 'wc_query', true );
// Store variables
$this->post__in = $post__in;
$this->post__in = $post__in;
$this->meta_query = $meta_query;
var_dump($q);
do_action( 'woocommerce_product_query', $q, $this );
}
@ -438,6 +438,22 @@ class WC_Query {
return $args;
}
/**
* Appends meta queries to an array.
*
* @access public
* @return void
*/
public function get_meta_query( $meta_query = array() ) {
if ( ! is_array( $meta_query ) )
$meta_query = array();
$meta_query[] = $this->visibility_meta_query();
$meta_query[] = $this->stock_status_meta_query();
return array_filter( $meta_query );
}
/**
* Returns a meta query to handle product visibility
*
@ -446,18 +462,20 @@ class WC_Query {
* @return array
*/
public function visibility_meta_query( $compare = 'IN' ) {
if ( is_search() ) $in = array( 'visible', 'search' ); else $in = array( 'visible', 'catalog' );
if ( is_search() )
$in = array( 'visible', 'search' );
else
$in = array( 'visible', 'catalog' );
$meta_query = array(
'key' => '_visibility',
'value' => $in,
'key' => '_visibility',
'value' => $in,
'compare' => $compare
);
return $meta_query;
}
/**
* Returns a meta query to handle product stock status
*

View File

@ -335,9 +335,7 @@ class WC_Shortcodes {
'order' => 'desc'
), $atts));
$meta_query = array();
$meta_query[] = $woocommerce->query->visibility_meta_query();
$meta_query[] = $woocommerce->query->stock_status_meta_query();
$meta_query = $woocommerce->query->get_meta_query();
$args = array(
'post_type' => 'product',
@ -643,6 +641,7 @@ class WC_Shortcodes {
$meta_query = array();
$meta_query[] = $woocommerce->query->visibility_meta_query();
$meta_query[] = $woocommerce->query->stock_status_meta_query();
$meta_query = array_filter( $meta_query );
$args = array(
'posts_per_page'=> $per_page,

View File

@ -85,7 +85,7 @@ class WC_Widget_Best_Sellers extends WP_Widget {
'no_found_rows' => 1,
);
$query_args['meta_query'] = array();
$query_args['meta_query'] = $woocommerce->query->get_meta_query();
if ( isset( $instance['hide_free'] ) && 1 == $instance['hide_free'] ) {
$query_args['meta_query'][] = array(
@ -96,9 +96,6 @@ class WC_Widget_Best_Sellers extends WP_Widget {
);
}
$query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();
$query_args['meta_query'][] = $woocommerce->query->visibility_meta_query();
$r = new WP_Query($query_args);
if ( $r->have_posts() ) {

View File

@ -83,14 +83,11 @@ class WC_Widget_Featured_Products extends WP_Widget {
<?php $query_args = array('posts_per_page' => $number, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product' );
$query_args['meta_query'] = array();
$query_args['meta_query'] = $woocommerce->query->get_meta_query();
$query_args['meta_query'][] = array(
'key' => '_featured',
'value' => 'yes'
);
$query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();
$query_args['meta_query'][] = $woocommerce->query->visibility_meta_query();
$r = new WP_Query($query_args);

View File

@ -79,9 +79,7 @@ class WC_Widget_Onsale extends WP_Widget {
$product_ids_on_sale = woocommerce_get_product_ids_on_sale();
$product_ids_on_sale[] = 0;
$meta_query = array();
$meta_query[] = $woocommerce->query->visibility_meta_query();
$meta_query[] = $woocommerce->query->stock_status_meta_query();
$meta_query = $woocommerce->query->get_meta_query();
$query_args = array(
'posts_per_page' => $number,

View File

@ -63,6 +63,7 @@ class WC_Widget_Random_Products extends WP_Widget {
}
$query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();
$query_args['meta_query'] = array_filter( $query_args['meta_query'] );
$query = new WP_Query( $query_args );

View File

@ -87,6 +87,7 @@ class WC_Widget_Recent_Products extends WP_Widget {
}
$query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();
$query_args['meta_query'] = array_filter( $query_args['meta_query'] );
$r = new WP_Query($query_args);

View File

@ -84,8 +84,8 @@ class WC_Widget_Recently_Viewed extends WP_Widget {
$query_args = array('posts_per_page' => $number, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'post__in' => $viewed_products, 'orderby' => 'rand');
$query_args['meta_query'] = array();
$query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();
$query_args['meta_query'] = array_filter( $query_args['meta_query'] );
$r = new WP_Query($query_args);

View File

@ -79,10 +79,7 @@ class WC_Widget_Top_Rated_Products extends WP_Widget {
$query_args = array('posts_per_page' => $number, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product' );
$query_args['meta_query'] = array();
$query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();
$query_args['meta_query'][] = $woocommerce->query->visibility_meta_query();
$query_args['meta_query'] = $woocommerce->query->get_meta_query();
$top_rated_posts = new WP_Query( $query_args );

View File

@ -178,6 +178,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Fix - Insert URL button when working with multiple variations.
* Fix - Undefined found_shipping_classes in flat rate shipping.
* Fix - Fix saving options for attribute taxonomies containing special chars.
* Fix - Prevent empty meta queries.
* Localization - Norwegian updates by Tore Hjartland
* Localization - Spanish updates by Laguna Sanchez
* Localization - Romanian updates by by Aurel Roman

View File

@ -15,9 +15,7 @@ $crosssells = $woocommerce->cart->get_cross_sells();
if ( sizeof( $crosssells ) == 0 ) return;
$meta_query = array();
$meta_query[] = $woocommerce->query->visibility_meta_query();
$meta_query[] = $woocommerce->query->stock_status_meta_query();
$meta_query = $woocommerce->query->get_meta_query();
$args = array(
'post_type' => 'product',

View File

@ -15,9 +15,7 @@ $upsells = $product->get_upsells();
if ( sizeof( $upsells ) == 0 ) return;
$meta_query = array();
$meta_query[] = $woocommerce->query->visibility_meta_query();
$meta_query[] = $woocommerce->query->stock_status_meta_query();
$meta_query = $woocommerce->query->get_meta_query();
$args = array(
'post_type' => 'product',