Im having trouble with my WordPress query on a project I do for a customer of us. Basically my problem is that I want to grab posts from the database with specified meta_keys. I want to get my posts (wpcompare) with the meta key '_price' and value between xx and yy. Works fine so far. Now I want to add filters for manufacturer, categories and tags. These filter values are all multiple, so that you can select multiple manufacturers. For example Canon and Nikon. So with the WP_MetaQuery I can filter multiple meta_keys and values, but I cant combine the queries with AND or OR.
Let me give you an example, how my query should work:
"Give me all the posts with post_type "wpcompare" where the meta_value _price is between 100 and 200 bucks, and where the manufacturer (_hersteller) is Canon OR Nikon OR Sony".
My head turns crazy, so please help me.
Thank you very much in advance :-)
Here is my Code:
if(isset($_POST) AND !empty($_POST))
{
$meta_query = array(
'relation' => 'AND',
array(
'key' => '_price',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
);
}
else
{
$meta_query = '';
}
$args = array(
'post_type' => 'wpcompare',
'post_status' => 'publish',
'paged' => $paged,
'meta_query' => $meta_query,
'posts_per_page' => ($per_page == false) ? 18 : $per_page,
'ignore_sticky_posts'=> true
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args)
$args = array(
'post_type' => 'posttypehere',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_price',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'somekey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'anotherkey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ($query->have_posts()) : $query->the_post();
echo $post_id = get_the_ID();
endwhile;
endif;
$meta_query = array(
'relation' => 'OR',
array(
'key' => '_price',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'somekey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'anotherkey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
);
Related
I am trying to use WC_Order_Query, to get all orders where a custom meta_key doesn't exist, is empty or equal to 0
I've tried like a lot of the stuff documented on this site, but nothing seems to work. It just returns all content, which is the opposite of what i'm trying to do.
This is what most people have recommended so far, but it doesn't seem to work as intended or maybe I am not seeing the issue here
$args = array(
'limit' => 9999,
'return' => 'ids',
'orderby' => 'date',
'order' => 'DESC',
'status' => 'processing',
'date_created' => '>='.$startdate,
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'order_printed',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => ''
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => 0
)
)
)
);
$query = new WC_Order_Query( $args );
$orders = $query->get_orders();
This ended up being the solution:
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => -1,
'post_status' => 'any',
'orderby' => 'the_date',
'order' => 'DESC',
'date_query' => array(
array(
'after' => $startdate . $starttime,
'inclusive' => true
)
),
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'order_printed',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => ''
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => 0
)
)
)
);
$query = new WP_Query( $args );
$query_posts = $query->get_posts();
Thanks a lot to Boris for the assistance.
I'm woocommerce 2.8, How can I count the products existing in the shop "on sale" for a specific category (ex: computers) ?
I found a way to count all products :
$count_posts = wp_count_posts( 'product' );
return $count_posts->publish;
But how can I get the number of products "on-sale" for a specific category ?
Thanks
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => here goes your category id, you can also go for slug
)
),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array(
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$query = new WP_Query($args);
$onSaleCount = $query->found_posts;
The above looks for simple and variable products on sale belonging to a category mentioned under tax_query.
I'm not sure if there's a better method, but this should work.
LE - Changed args to exclude products out of stock as per comments.
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => here goes your category id, you can also go for slug
)
),
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array(
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
),
array(
'key' => '_stock_status',
'value' => 'instock'
),
)
);
We have set up custom post types for real estate properties and a search form to search through these custom post types using price, city, newest, etc. We are using query variable strings/if statements to display the results, but we are having trouble with the sort options. We have three sort options in place : Newest, Price Low-High, and Price High-Low.
When the default results are displayed, the sort does not work correctly, but when a city is set in the search dropdown, the sort works perfectly.
For the city search, we have this code in place:
if (isset($_POST['city'])) {
$qs_city = get_query_var('city');
}
else {
$qs_city = " ";
I believe that this is due to the else portion of the code above. We need some sort of wildcard that allows all cities listed to be listed if a city is not set. We have tried several different things, but nothing seems to work. I'm fairly new to this, so I can better clarify any of the above, if necessary.
This is the code used to sort:
if (isset($_POST['sort'])) {
$qs_sort = get_query_var('sort');
switch ($qs_sort) {
case 'newest':
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'L_StatusDate',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
break;
case 'price-low':
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'L_SystemPrice',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
break;
case 'price-high':
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'L_SystemPrice',
'order' => 'DESC',
'meta_query' => array(
//'relation' => 'AND',
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
break;
}
}
else {
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(
//'relation' => 'AND',
array(
'key' => 'L_StatusDate',
'orderby' => 'DESC'
//set default to sort by newest
),
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
}
?>
The main question for your situation is "what kind of application will sort the results?". It looks like you use some kind on database. But which one? If it's SQL-like database, - just replace $qs_city = " " with $qs_city = "%";
I want to retrieve result from two different custom post type.I queried for tax_query , meta_query and another meta_query but retriving no result.there should be one result.
Below is the codes,
$args = array(
'post_type' => 'listings',
's' => get_query_var( 's' ),
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
),
'tax_query' => array(
'relation' => 'OR',
),
'posts_per_page' => 15,
);
if(isset($_GET['city']) && !empty($_GET['city'])){
$args['tax_query'][] = array(
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $selected_city,
'relation' => 'OR',
),
);
}
if(isset($_GET['cuisine'])){
$args['tax_query'][] = array(
array(
'taxonomy' => 'cuisine',
'field' => 'slug',
'terms' => $selected_cuisine,
'relation' => 'OR',
),
);
}
if(isset($_GET['st'])){
$args['meta_query'][] = array(
array(
'key' => 'restaurent_location',
'value' => $location,
'compare' => 'LIKE',
),
);
}
if(isset($_GET['has_takeaway'])){
$args['meta_query'][] = array(
array(
'key' => 'has_resturent_takeaway',
'value' => $takeaway,
'compare' => '=',
),
);
}
$arg2 = array(
'post_type' => 'reviews',
'meta_query' => array(
'relation' => 'OR',
),
);
if(isset($_GET['rating_range'])){
$args2['meta_query'][] = array(
array(
'key' => 'review_rating',
'value' => array( $rating_range[0], $rating_range[1] ),
'compare' => 'BETWEEN',
),
);
}
$query = new WP_Query( array_merge($args, $args2) );
get_query_var( 's' ) should be just $s after you've set $s. Right now it's not searching for anything.
I've got some custom fields as meta data for a new post type - Properties (for an estate agents), so want to search by number of bedrooms, min/max value and location. I have a form with multiple drop-downs for each of these fields:
location, min_value, max_value, bedrooms
Also, I have meta boxes on the posts themselves, so one for price, bedrooms, location, and a taxonomy type of property_type - rent, sale, and commerical.
I've found this piece of code online but not sure how to manipulate it so it takes whatever value the form takes?
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'location',
'value' => '[LOCATION HERE]',
'compare' => 'NOT LIKE'
),
array(
'key' => 'price',
'value' => '[PRICE HERE FROM FORM]',
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$query = new WP_Query( $args );
Also, I understand the search query goes on function.php but do I call it from where the form is, or where the results are outputted? ie. my homepage or my searchpage?
Hope someone can help
use this code
$args = array(
'post_type' => 'Properties',
'meta_query' => array(
array(
'key' => 'location',
'value' => '[LOCATION HERE]',
'compare' => 'LIKE'
),
array(
'key' => 'min_value',
'value' => '[min value here]',
'type' => 'numeric',
'compare' => 'BETWEEN'
)
array(
'key' => 'max_value',
'value' => '[max value here]',
'type' => 'numeric',
'compare' => 'BETWEEN'
)
array(
'key' => 'bedrooms',
'value' => '[bedroom here]',
'compare' => 'LIKE'
),
)
);
$query = new WP_Query( $args );
and You have to call this in your searchpage....
Thanks for help Yogesh, I modified your answer to get this which seems to work:
<?php $args = array(
'post_type' => 'Property',
'property_type'=>$_GET['type'],
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_property_info_location',
'value' => Cuztom::uglify($_GET['location']),
),
array(
'key' => '_property_info_bedrooms',
'value' => $_GET['bedrooms'],
),
array(
'key' => '_property_info_price',
'value' => $_GET['max_value'],
'compare' => '<=',
'type' => 'numeric',
),
array(
'key' => '_property_info_price',
'value' => $_GET['min_value'],
'compare' => '>=',
'type' => 'numeric',
),
),
);
$the_query = new WP_Query( $args );
?>