Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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',
                        array(
                            'taxonomy' => 'age',
                            'field' => 'id',
                            'terms' => $age,
                            'operator' => 'IN'
                        ),
                        array(
                            'taxonomy' => 'price',
                            'field' => 'id',
                            'terms' => $price,
                            'operator' => 'IN'
                        ),
                        array(
                            '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

1 Answer 1

up vote 1 down vote accepted

You want to use the Haversine formula

http://en.wikipedia.org/wiki/Haversine_formula

This plugin might be of interest to you:

http://wordpress.org/extend/plugins/wp-geoposts/

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

it uses the Haversine formula here:

http://plugins.svn.wordpress.org/wp-geoposts/trunk/query.php

share|improve this answer

Your Answer

 
discard

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.