Skip to content

Instantly share code, notes, and snippets.

@nielsvr
Created November 11, 2013 12:10
Show Gist options
  • Save nielsvr/7412291 to your computer and use it in GitHub Desktop.
Save nielsvr/7412291 to your computer and use it in GitHub Desktop.
SQL Query to calculate distance on a post/meta object in WordPress
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