Generating a radius search on postmeta-metavalue and adding a taxonomy to query
This is an old SQL statement found on here from way back in 2011, but its the closet to doing what I need I could find and work on. Although it does exactly what I need, displays properties within a given radius of currently displayed property, I need to add the taxonomy for the contract type in so it only shows properties, given radius, contract type.
I've tried all sorts, but cant quite figure out how to join the table for terms/term relationships to do this.
Any help would be most appreciated.
This is the code I have at the moment (Messy I know).
SELECT DISTINCT `t`.`ID`,
6371 * 2 * ASIN( SQRT( POWER( SIN( ( '13.727561' - `t`.`latitude` ) * pi() / 180 / 2), 2 ) + COS( '13.727561' * pi() / 180) * COS( `t`.`latitude` * pi() / 180 ) * POWER( SIN( ( '100.581708' - `t`.`longitude` ) * pi() / 180 / 2 ), 2 ) ) ) AS `distance`
FROM (
SELECT `wp_posts`.`ID`,
MAX(CASE WHEN ``.`meta_key` = '_property_longitude' THEN `wp_postmeta`.`meta_value` END ) AS `longitude`,
MAX(CASE WHEN `wp_postmeta`.`meta_key` = '_property_latitude' THEN `wp_postmeta`.`meta_value` END ) AS `latitude`
FROM `wp_term_taxonomy`, `wp_posts`
LEFT JOIN `wp_postmeta` ON ( `wp_posts`.`ID` = `wp_postmeta`.`post_id` )
WHERE `wp_posts`.`post_status` = 'publish'
AND `wp_term_taxonomy`.`term_id` = '36'
AND `wp_posts`.`post_type` = 'dt_properties'
AND `wp_postmeta`.`post_id` != '2553'
GROUP BY `wp_posts`.`ID`
HAVING `longitude` BETWEEN '100.485824158' AND '100.677591842' AND `latitude` BETWEEN '13.6344160725' AND '13.8207059275') AS `t`
HAVING distance 6.427
ORDER BY distance ASC;
Topic location-search custom-taxonomy custom-post-types Wordpress
Category Web