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',
                ) );
  • Please provide more detail, including the code your are currently trying to use.
    – s_ha_dum
    Feb 9, 2013 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, 2013 at 19:48

1 Answer 1


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:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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