WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got table in the WordPress database that has lat & lng stored and I'd like to add a sort feature on the results page 'Sort by Distance' that gets the locations that are closer to your provided lat & lng. I'm using WP_Query, it needs to work with it. Thanks :)

                $my_query = new WP_Query( array(
                'post_status' => 'publish',
                'post_type' => 'post', 
                'category__in' => $category,
                'tax_query' => array(
                        'relation' => 'AND',
                            'taxonomy' => 'age',
                            'field' => 'id',
                            'terms' => $age,
                            'operator' => 'IN'
                            'taxonomy' => 'price',
                            'field' => 'id',
                            'terms' => $price,
                            'operator' => 'IN'
                            'taxonomy' => 'group',
                            'field' => 'id',
                            'terms' => $group
                'posts_per_page' => '-1',
                ) );
share|improve this question
Please provide more detail, including the code your are currently trying to use. – s_ha_dum Feb 9 '13 at 19:45
Just a basic WP_Query function. I have the lat & lng of the user & the lat & lng of the post stored in a variable. Editing the post with the current code. – Stephen Feb 9 '13 at 19:48
up vote 2 down vote accepted

You want to use the Haversine formula


This plugin might be of interest to you:


Provides WP_GeoQuery an extended WP_Query class for doing distance based and geo-aware queries.

it uses the Haversine formula here:


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.