Skip to content

Instantly share code, notes, and snippets.

View sreigle's full-sized avatar

Sam Reigle sreigle

  • Minneapolis/St. Paul, MN
View GitHub Profile
@sreigle
sreigle / radius-search.php
Created August 27, 2015 15:26
Radius search of post meta latitude and longitude
<?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'