I’m building a houses class and I want to filter these houses with a query. But don’t get any results:
Example house:
Min price: 10
Max price (field not required): empty
I’ve a price filter set on Min price 6 – Max price 15
I use this query to get the results, but it is not working.
new WP_Query(
[
'post_type' => 'properties',
'meta_query' => [
'relation' => 'AND',
'min_price' => [
'relation' => 'AND',
[
'key' => 'min_price',
'value' => 6,
'compare' => '>=',
'type' => 'NUMERIC'
],
],
'max_price' => [
'relation' => 'AND',
[
'key' => 'max_price',
'value' => 11,
'compare' => '<=',
'type' => 'NUMERIC'
],
[
'key' => 'max_price',
'value' => '',
'compare' => '!=',
],
],
],
]
);
I’m also wondering how to get this working with the required min value and the optional max value. I’ve the same problem with bedrooms, bathrooms etc.
Thanks in advance!
This is my simple example for testing purposes, but it doesn’t work. The min_price is not a problem but when i add the max_price it doesn’t work.
'relation' => 'AND',
[
'key' => 'min_price',
'value' => 6,
'type' => 'NUMERIC',
'compare' => '>='
],
[
'key' => 'max_price',
'value' => 13,
'type' => 'NUMERIC',
'compare' => '<='
]
SQL QUERY
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1 AND (
(
( wp_postmeta.meta_key = 'min_price' AND CAST(wp_postmeta.meta_value AS SIGNED) >= '6' )
AND
( mt1.meta_key = 'max_price' AND CAST(mt1.meta_value AS SIGNED) <= '13' )
)
) AND wp_posts.post_type = 'properties' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 999
Update CAUSES
'relation' => 'OR',
[
'key' => 'min_price',
'value' => 6,
'type' => 'NUMERIC',
'compare' => '>='
],
[
'key' => 'max_price',
'type' => 'NUMERIC',
'value' => [6, 13],
'compare' => 'BETWEEN'
]