I've got lots of posts with promotional prices and the rest have an exact price. I just wanted to make sure that if promotional price exists, use the promotional price as the target data when I try to submit a value from the input range else use the real price.
So in my form, I have this slider:
<input class="price-select" name="price" value="5000" type="range" min="0" max="5000">
and my query looks like this, but it's not working.
$args = [
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => []
];
if (isset($date)) {...}
if (isset($destination)) {...}
if (isset($price)) {
array_push($args['meta_query'], [
'key' => 'promotion_price',
'value' => $price,
'type' => 'numeric',
'compare' => '<=',
]);
array_push($args['meta_query'], [
'key' => 'exact_price',
'value' => $price,
'type' => 'numeric',
'compare' => '<=',
]);
}
$getPackages = new WP_Query( $args );
This one works fine but it doesn't work with what I wanted, as I can't think of any solution.
I tried using BETWEEN
and IN
but it's not working. I'm not sure how it works in this situation.
Adding more information:
I've got lots of search filters and the way I added them is by using array_push
. I tried doing this code below but it affects the other search filter and returned empty. Tried doing the 'relation' => 'OR'
, it works for the rest of the search filters but the price and promotion price are not working.
$args = [
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => [
'relation' => 'AND'
]
];
Any thoughts would be appreciated! Thanks!
Array dump from $args
Without Relation on meta_query
array(6) {
["post_type"]=>
string(15) "post"
["posts_per_page"]=>
int(4)
["post_status"]=>
string(7) "publish"
["tax_query"]=>
array(0) {
}
["meta_query"]=>
array(2) {
[0]=>
array(4) {
["key"]=>
string(34) "promotion_price"
["value"]=>
string(3) "439"
["type"]=>
string(7) "numeric"
["compare"]=>
string(2) "<="
}
[1]=>
array(4) {
["key"]=>
string(24) "exact_price"
["value"]=>
string(3) "439"
["type"]=>
string(7) "numeric"
["compare"]=>
string(2) "<="
}
}
["paged"]=>
int(1)
}
With Relation on meta_query
array(6) {
["post_type"]=>
string(15) "post"
["posts_per_page"]=>
int(4)
["post_status"]=>
string(7) "publish"
["tax_query"]=>
array(0) {
}
["meta_query"]=>
array(3) {
["relation"]=>
string(2) "OR"
[0]=>
array(4) {
["key"]=>
string(34) "promotion_price"
["value"]=>
string(3) "456"
["type"]=>
string(7) "numeric"
["compare"]=>
string(2) "<="
}
[1]=>
array(4) {
["key"]=>
string(24) "exact_price"
["value"]=>
string(3) "456"
["type"]=>
string(7) "numeric"
["compare"]=>
string(2) "<="
}
}
["paged"]=>
int(1)
}
All of my array arguments and search filters:
$args = [
'post_type' => 'post',
'posts_per_page' => 4,
'post_status' => 'publish',
'tax_query' => [],
'meta_query' => [],
'paged' => 1
];
if (isset($dates)) {
array_push($args['meta_query'], [
'key' => 'dates',
'compare' => 'IN',
'value' => $dates,
]);
}
if (isset($price)) {
array_push($args['meta_query'], [
'key' => 'promotion_price',
'value' => $price,
'type' => 'numeric',
'compare' => '<=',
]);
array_push($args['meta_query'], [
'key' => 'exact_price',
'value' => $price,
'type' => 'numeric',
'compare' => '<=',
]);
}
if (isset($packages)) {
foreach ($packages as $package) {
array_push($args['tax_query'], [
'taxonomy' => 'tax_1',
'field' => 'term_id',
'terms' => $package,
]);
}
}
if (isset($durations)) {
foreach ($durations as $duration) {
array_push($args['tax_query'], [
'taxonomy' => 'tax_2',
'field' => 'term_id',
'terms' => $duration,
]);
}
}
if (isset($departures)) {
foreach ($departures as $departure) {
array_push($args['tax_query'], [
'taxonomy' => 'tax_3',
'field' => 'term_id',
'terms' => $departure,
]);
}
}
$getPackages = new WP_Query( $args );
$price
?, you use it but you never declared it. add this$price = filter_input(INPUT_POST, 'price', FILTER_SANITIZE_STRING)
right inside theif ()
check$_GET['price']
. Updated my post. Thanks!print_r
orvar_dump
of your$args
, I think the relation of all the meta_query (AND/OR) maybe causes unexpected results. You will probably need to nest some of the meta queries