Created
November 11, 2013 12:10
-
-
Save nielsvr/7412291 to your computer and use it in GitHub Desktop.
SQL Query to calculate distance on a post/meta object in WordPress
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
SELECT *, ( 3959 * ACOS( COS( RADIANS(18.204540500000) ) | |
* COS( RADIANS( latitude ) ) | |
* COS( RADIANS( longitude ) | |
- RADIANS(-66.450958500000) ) | |
+ SIN( RADIANS(18.204540500000 ) ) | |
* SIN( RADIANS( latitude ) ) | |
) | |
) AS distance | |
FROM | |
(SELECT ID, | |
post_title, | |
post_author, | |
MAX(CASE WHEN meta_key='venue_lat' THEN meta_value END) latitude, | |
MAX(CASE WHEN meta_key='venue_lng' THEN meta_value END) longitude | |
FROM `wp_posts` | |
LEFT JOIN `wp_postmeta` | |
ON ID=post_id | |
WHERE post_type='spot' | |
AND (meta_key='venue_lat' OR meta_key='venue_lng') | |
GROUP BY ID, post_title, post_author | |
ORDER BY ID ASC) AS A |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment