$lng2 = (double) $search_lng + (int) $radius / abs(cos(deg2rad((double) $search_lat)) * 69); $sqlsquareradius = "\n\t\t\tSELECT\n\t\t\t\t`" . $wpdb->prefix . self::$tablename . "`.`post_id`,\n\t\t\t\t`" . $wpdb->prefix . self::$tablename . "`.`lat`,\n\t\t\t\t`" . $wpdb->prefix . self::$tablename . "`.`lng`\n\t\t\tFROM\n\t\t\t\t`" . $wpdb->prefix . self::$tablename . "`\n\t\t\tWHERE\n\t\t\t\t`" . $wpdb->prefix . self::$tablename . "`.`post_type` = '{$post_type}'\n\t\t\tAND\n\t\t\t\t`" . $wpdb->prefix . self::$tablename . "`.`lat` BETWEEN '{$lat1}' AND '{$lat2}'\n\t\t\tAND\n\t\t\t\t`" . $wpdb->prefix . self::$tablename . "`.`lng` BETWEEN '{$lng1}' AND '{$lng2}'\n\t\t\t"; // End $sqlsquareradius // Create sql for circle radius check $sqlcircleradius = "\n\t\t\tSELECT\n\t\t\t\t`t`.`post_id`,\n\t\t\t\t3956 * 2 * ASIN(\n\t\t\t\t\tSQRT(\n\t\t\t\t\t\tPOWER(\n\t\t\t\t\t\t\tSIN(\n\t\t\t\t\t\t\t\t( " . (double) $search_lat . " - `t`.`lat` ) * pi() / 180 / 2\n\t\t\t\t\t\t\t), 2\n\t\t\t\t\t\t) + COS(\n\t\t\t\t\t\t\t" . (double) $search_lat . " * pi() / 180\n\t\t\t\t\t\t) * COS(\n\t\t\t\t\t\t\t`t`.`lat` * pi() / 180\n\t\t\t\t\t\t) * POWER(\n\t\t\t\t\t\t\tSIN(\n\t\t\t\t\t\t\t\t( " . (double) $search_lng . " - `t`.`lng` ) * pi() / 180 / 2\n\t\t\t\t\t\t\t), 2\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\t) AS `distance`\n\t\t\tFROM\n\t\t\t\t({$sqlsquareradius}) AS `t`\n\t\t\tHAVING\n\t\t\t\t`distance` <= " . (int) $radius . "\n\t\t\tORDER BY `distance` {$orderby}\n\t\t\t"; // End $sqlcircleradius return $wpdb->get_col($sqlcircleradius); } // End function getPostIDsOfInRange /** * Get all post id's ordered by distance from given point * * @param string $post_type The post type of posts you are searching * @param float $search_lat The latitude of where you are searching * @param float $search_lng The Longitude of where you are searching * @param string $orderby What order do you want the ID's returned as? ordered by distance ASC or DESC? * @return array $wpdb->get_col() array of ID's in ASC or DESC order as distance from point */ public static function getPostIDsByRange($post_type, $search_lat = 51.499882, $search_lng = -0.126178, $orderby = "ASC") { global $wpdb; // Dont forget to include wordpress DB class // Create sql for distance check $sqldistancecheck = "\n\t\t\tSELECT\n\t\t\t\t`" . $wpdb->prefix . self::$tablename . "`.`post_id`,\n\t\t\t\t3956 * 2 * ASIN(\n\t\t\t\t\tSQRT(\n\t\t\t\t\t\tPOWER(\n\t\t\t\t\t\t\tSIN(\n\t\t\t\t\t\t\t\t( " . (double) $search_lat . " - `" . $wpdb->prefix . self::$tablename . "`.`lat` ) * pi() / 180 / 2\n\t\t\t\t\t\t\t), 2\n\t\t\t\t\t\t) + COS(\n\t\t\t\t\t\t\t" . (double) $search_lat . " * pi() / 180\n\t\t\t\t\t\t) * COS(\n\t\t\t\t\t\t\t`" . $wpdb->prefix . self::$tablename . "`.`lat` * pi() / 180\n\t\t\t\t\t\t) * POWER(\n\t\t\t\t\t\t\tSIN(\n\t\t\t\t\t\t\t\t( " . (double) $search_lng . " - `" . $wpdb->prefix . self::$tablename . "`.`lng` ) * pi() / 180 / 2\n\t\t\t\t\t\t\t), 2\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\t) AS `distance`\n\t\t\tFROM\n\t\t\t\t`" . $wpdb->prefix . self::$tablename . "`\n\t\t\tWHERE\n\t\t\t\t`" . $wpdb->prefix . self::$tablename . "`.`post_type` = '{$post_type}'\n\t\t\tORDER BY `distance` {$orderby}\n\t\t\t"; // End $sqldistancecheck return $wpdb->get_col($sqldistancecheck); } } } sc_GeoDataStore::init();
function around_alter_query($query) { global $wp_query, $sc_gds; if (isset($_GET['latitude']) && isset($_GET['longitude']) && isset($_GET['distance'])) { //Clean GET parameters $latitude = floatval($_GET['latitude']); if (!is_numeric($latitude)) { $latitude = 0; } $longitude = floatval($_GET['longitude']); if (!is_numeric($longitude)) { $longitude = 0; } $distance = floatval($_GET['distance']); if (!is_numeric($distance)) { $distance = 0; } // Load instance of GeoDataStore if (!isset($sc_gds)) { $sc_gds = new sc_GeoDataStore(); } // Just get the ID's of posts in range $ids = (array) $sc_gds->getPostIDsOfInRange("post", $distance, $latitude, $longitude); // We we have no results then set an array just one that will trigger no posts found. if (empty($ids)) { $wp_query->set('post__in', array(0)); } else { $wp_query->set('post__in', $ids); $wp_query->set('orderby', 'post__in'); } } }