Created
August 27, 2015 15:26
-
-
Save sreigle/741ed24afe25bb57478f to your computer and use it in GitHub Desktop.
Radius search of post meta latitude and longitude
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function radius_search($lat, $long, $distance = 100){ | |
global $wpdb; | |
return $wpdb->get_results("SELECT DISTINCT item_latitude.post_id, | |
wp_posts.post_title, | |
((ACOS(SIN($lat * PI() / 180) * SIN(item_latitude.meta_value * PI() / 180) + COS($lat * PI() / 180) * COS(item_latitude.meta_value * PI() / 180) * COS(($long - item_longitude.meta_value) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance | |
FROM wp_postmeta AS item_latitude | |
LEFT JOIN wp_postmeta as item_longitude ON item_latitude.post_id = item_longitude.post_id | |
INNER JOIN wp_posts ON wp_posts.ID = item_latitude.post_id | |
WHERE item_latitude.meta_key = 'latitude' AND item_longitude.meta_key = 'longitude' | |
HAVING distance < $distance | |
ORDER BY distance ASC;"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment