3

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'
]
  • Why would a house have a min and max price? Normally it would have a single price, and your search criteria would be a min/max range. – Milo Mar 3 '18 at 16:27
  • They also sell projects. So the price can vary depending on square feet, location & other differences. – John McGuinnes Mar 3 '18 at 16:50
0

Depending on the way your script is recording max_price, it isn't really a meta_key/meta_value on postmeta table.

Did you try changin max_price clause to something like this?

['max_price'] => [
    'relation' => 'OR',
    [
        'key' => 'max_price',
        'value' => 11,
        'compare' => '<=',
    ],
    [
        'key' => 'max_price',
        'compare' => 'NOT EXISTS',
    ],
],
  • I've tried this, but it still does'nt show me any results. I filled both fields (min and max price). To see if i get any results. I also altered the query to make it less complex for testing purposes. I've edited my example above. – John McGuinnes Mar 4 '18 at 16:06
  • @JohnMcGuinnes did you try print_r the WP_Query object and see how is the SQL Query? It is the request property if I can remember. That can give you a hint. – Felipe Elia Mar 4 '18 at 19:27
  • I've added the sql query and i see where it goes wrong. But i don't know the solution, this is still a bit hard for me. It goes wrong here <= '13'. 13 is in between 10 and 15. – John McGuinnes Mar 4 '18 at 19:58
  • 1
    Actually you need to change both compare clauses, right? WordPress deals with it like post_meta_value COMPARE query_value, i.e., in max_price we end up with post_meta_value <= query_value and that's not what you want. Supposing your user is looking for a min=6 and max=15, and you have a house with min_price=4 and max_price=20. You need to retrieve houses with min_price<=6 ('cause 4<=6) and max_price>=15 (cause 20>=15). – Felipe Elia Mar 4 '18 at 21:52
  • Thanks, i will look into this and hopefully i can do this with a meta_query instead of sql. – John McGuinnes Mar 7 '18 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.