I'll admit, I'm not a PHP expert, nor am I familiar with all of the WordPress functions and hooks. I've been stuck on an issue, searching for help without much success, and I was hoping that someone could help point me in the right direction.

I have a form. The user selects a specialty, location, and distance (10 miles, 20, 50, etc). The results are queried from a custom post type and filtered to match the specialty. The posts have their latitude and longitude in the meta data but I am not sure how to bring this into the query. I want the posts to only show if their latitude/longitude is within proximity from the specified distance. I have the functionality written already to get their distance. The trouble I'm having is with the query and displaying the data properly.

Here are my current query arguments:

$args = array(
    'post_type' => 'team-members',
    'posts_per_page' => -1,
    'meta_query' => array(
            'key' => 'specialties',
            'value' => $_GET['specialization'],
            'compare' => 'LIKE'
$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();

This currently will display all the posts with the matching specialization, however, does not include anything involving the distance. So what I initially did was then set a conditional to check if the post was within the distance specified (don't worry about these variables, they are displaying the correct data):

if ( $distance < $_GET['within'] ) {

The issue is that this condition is not factored into the query, and I am unable to sort by distance. Additionally, I have to set a counter in order to get a correct post "count", plus it just feels hacked together. I feel like there is a foundational error here, and I can't pinpoint it.

I hope this information was sufficient, and if you need anything clarified, please comment. I didn't want to overwhelmingly provide more than was necessary.

  • 4
    There's a way to add a geo_query attribute support to the WP_Query. I experimented with this here on GitHub, maybe that can help you along the way?
    – birgire
    Jun 18, 2015 at 21:48
  • in database what you have stored latitude-longitude or Distance ?
    – Mitul
    Jun 19, 2015 at 10:40
  • Instead of using the WP_Query use the custom query let me know if you need any help for to perform custom query, i need your meta key for that
    – Mitul
    Jun 19, 2015 at 11:05
  • @birgire that looks interesting, thanks. Although, I already have the calculations for the latitude, longitude, and distance, will this be easily implementable?
    – user33056
    Jun 20, 2015 at 0:42
  • @Mitul I have the latitude and longitude stored in the database as post meta data. The distance is calculated by calling a PHP function within The Loop. $distance = round(calc_distance($input_lat, $input_lng, $result_lat, $result_long, "M"));
    – user33056
    Jun 20, 2015 at 0:44

2 Answers 2


here is the mysql query you can use to get the result having distance less then your value.

SELECT * , pm1.meta_value  as latitude, pm2.meta_value as longitude ,    
      (((acos(sin(([latitude]*pi()/180)) * 
          sin((pm1.meta_value * pi()/180)) + cos(([latitude]*pi()/180)) * 
          cos((pm1.meta_value * pi()/180)) * cos((([longitude] - pm2.meta_value)* 
      ) as distance

FROM wp_posts p
JOIN wp_postmeta pm1 ON pm1.post_id = p.ID AND pm1.meta_key = 'latitude'
JOIN wp_postmeta pm2 ON pm2.post_id = p.ID AND pm2.meta_key = 'longitude'
having distance < $_GET['within']

It will return all the post with distance is less then your $_GET['within'] value.

  • Where are you getting this info? I have all of the data I need, I just need to rewrite my query
    – user33056
    Jun 22, 2015 at 22:46
  • @rpl great let me know i can help you
    – Mitul
    Jun 23, 2015 at 4:28
  • What? I'm not sure what you mean.
    – user33056
    Jun 24, 2015 at 20:52
  • @rpl use the query and let me know if you have trouble to get the data from database
    – Mitul
    Jun 26, 2015 at 4:57

It seems that you could use Types and Views plugins (with Views "Parametric search" feature). Views is commercial, but unless you are building your personal website, I think it's worth it.

Nice writeup about those plugins is, for example, here: http://www.neilcurtis.me/guides/best-wordpress-plugins/

  • I don't see why a plugin would be necessary. I just have to rewrite my query a bit, it seems...
    – user33056
    Jun 22, 2015 at 21:04
  • I am definitely not saying it is necessary, just proposing an alternative solution... Lot of people would prefer buying a plugin before having to write their own custom PHP code. I had no idea which one are you or what else are you trying to build on your website. I am sorry about the downvote, but okay...
    – zaantar
    Jun 23, 2015 at 23:07
  • I didn't downvote you to be clear. I just feel that my query is close and do not want to have to resort to using a plugin. I try to avoid most plugins, unless I know it's something that does exactly what I need and only that. Thank you for your input.
    – user33056
    Jun 24, 2015 at 20:52
  • I see. No problem at all :) It's true that I was proposing solution to bigger (and thus different) problem than you described.
    – zaantar
    Jun 25, 2015 at 21:57

Your Answer

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