1

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',
                ) );
2
  • 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

2

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

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.