Skip to content

Instantly share code, notes, and snippets.

@gamaup
Last active July 25, 2017 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamaup/b33e06ff56ebc019ecf96e2d8fac2774 to your computer and use it in GitHub Desktop.
Save gamaup/b33e06ff56ebc019ecf96e2d8fac2774 to your computer and use it in GitHub Desktop.
Get nearby location based on lat-long set on postmeta.
set @latitude = xxx; — center latitude
set @longitude = xxx; — center longitude
set @distance = xx; — search distance (miles)
select p.ID, p.post_name, ((ACOS(SIN(@latitude * PI() / 180) * SIN('latitude.meta_value' * PI() / 180) + COS(@latitude * PI() / 180) * COS('latitude.meta_value' * PI() / 180) * COS((@longitude – 'longitude.meta_value') * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance
from wp_posts p
left join wp_postmeta latitude on latitude.post_id = p.ID and latitude.meta_key = '_latitude'
left join wp_postmeta longitude on longitude.post_id = p.ID and longitude.meta_key = '_longitude'
having distance < @distance;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment