wp query - WP_Query min and max values

admin2025-01-07  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'
]

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'
]
Share Improve this question edited Mar 8, 2018 at 20:27 John McGuinnes asked Mar 3, 2018 at 16:13 John McGuinnesJohn McGuinnes 711 silver badge5 bronze badges 2
  • 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 Commented Mar 3, 2018 at 16:27
  • They also sell projects. So the price can vary depending on square feet, location & other differences. – John McGuinnes Commented Mar 3, 2018 at 16:50
Add a comment  | 

1 Answer 1

Reset to default 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',
    ],
],
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260065a636.html

最新回复(0)