This is my query:
SELECT wp_posts.ID, wp_posts.post_title, lat.meta_value AS latitude, lng.meta_value AS longitude, info1.meta_value AS street_no, info2.meta_value AS street, info3.meta_value AS street_suffix, price.meta_value AS price,
( 3959 * acos( cos( radians( '{$latitude}' ) ) * cos( radians( lat.meta_value ) ) * cos( radians( lng.meta_value ) - radians( '{$longitude}' ) ) + sin( radians( '{$latitude}' ) ) * sin( radians( lat.meta_value ) ) ) ) AS distance
FROM wp_posts
LEFT JOIN wp_postmeta AS lat
ON lat.post_id = wp_posts.ID
AND lat.meta_key = 'property_lat'
LEFT JOIN wp_postmeta AS lng
ON lng.post_id = wp_posts.ID
AND lng.meta_key = 'property_long'
LEFT JOIN wp_postmeta AS info1
ON info1.post_id = wp_posts.ID
AND info1.meta_key = 'street_no'
LEFT JOIN wp_postmeta AS info2
ON info2.post_id = wp_posts.ID
AND info2.meta_key = 'street'
LEFT JOIN wp_postmeta AS info3
ON info3.post_id = wp_posts.ID
AND info3.meta_key = 'street_suffix'
LEFT JOIN wp_postmeta AS price
ON price.post_id = wp_posts.ID
AND price.meta_key = 'price_current'
WHERE lat.meta_key IS NOT NULL
HAVING distance < 30
LIMIT 0, 20
I need to update the WHERE
statemtent to take into account price_min
and price_max
values… only returning results WHERE price is >= {$price_min} AND <= {$price_max}
I hope that makes sense…
Assuming you are only getting one row per id, I would suggest you rewrite your query as an aggregation:
This query is untested, so it may have syntax errors.
With this structure, you can just add:
to the outer query. I am assuming you mean price_current, since price is not in the original query.