Skip to content

Instantly share code, notes, and snippets.

@thierrypigot
Created June 10, 2015 18:44
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 thierrypigot/bed4bf6b5ef6fe92a92f to your computer and use it in GitHub Desktop.
Save thierrypigot/bed4bf6b5ef6fe92a92f to your computer and use it in GitHub Desktop.
Requête de récupération des posts en fonction de coordonnées. Les posts sont dans une table avec : post_id, lat, lng
/**
* 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
*/
function fuji_revendeur_getPostIDsByRange( $search_lat = 51.499882, $search_lng = -0.126178, $orderby = "ASC", $limit = 10, $services = false )
{
global $wpdb;// Dont forget to include wordpress DB class
$table = $wpdb->prefix .'geodatastore';
$join = "WHERE p.`post_status` = 'publish'";
if( $services )
{
$join = "INNER JOIN `". $wpdb->prefix ."term_relationships` t ON g.`post_id` = t.`object_id` WHERE p.`post_status` = 'publish' AND t.`term_taxonomy_id` = $services ";
}
// Create sql for distance check
$sqldistancecheck = "
SELECT
g.`post_id`,
3956 * 2 * ASIN(
SQRT(
POWER(
SIN(
( ".(float) $search_lat." - g.`lat` ) * pi() / 180 / 2
), 2
) + COS(
".(float) $search_lat." * pi() / 180
) * COS(
g.`lat` * pi() / 180
) * POWER(
SIN(
( ".(float) $search_lng." - g.`lng` ) * pi() / 180 / 2
), 2
)
)
) AS `distance`
FROM
`" . $table . "` g
INNER JOIN `". $wpdb->prefix ."posts` p ON g.`post_id` = p.`ID`
". $join ."
ORDER BY
`distance` {$orderby}
LIMIT
0, {$limit}
"; // End $sqldistancecheck
return $wpdb->get_results( $sqldistancecheck );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment