0

I've been trying to figure out a way to do radius search in WP and decided upon a simpler way than big queries, I'm having problems returning results though

I've got a function called bar_get_nearby() that calculates the latitude/longitude max/min for a given postcode, so that I can then find all entries inside that max/min value.

// GET POSTCODES LAT/LNG
$geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$_POST['location'].'&sensor=false');
$decode = json_decode($geo);
$latlon = $decode->results[0]->geometry->location->lat.','.$decode->results[0]->geometry->location->lng;
$dist = bar_get_nearby($decode->results[0]->geometry->location->lat, $decode->results[0]->geometry->location->lng);

// CHECK THEY WORK
echo $dist['max_latitude'].','.$dist['min_latitude'].'<--<br />';
echo $dist['max_longitude'].','.$dist['min_longitude'].'<---<br />';

// CREATE META QUERIES
$location_LAT = array('key' => 'latitude', 'value' => $dist['max_latitude'], 'compare' => '>=');
$location_LAT2 = array('key' => 'latitude', 'value' => $dist['min_latitude'], 'compare' => '<=');
$location_LNG = array('key' => 'longitude', 'value' => $dist['max_longitude'], 'compare' => '>=');
$location_LNG2 = array('key' => 'longitude', 'value' => $dist['min_longitude'], 'compare' => '<=');

I've also tried

$latitude = array('key' => 'latitude', 'value' => array($dist['max_latitude'],$dist['min_latitude']), 'compare' => 'BETWEEN');
$longitude = array('key' => 'longitude', 'value' => array($dist['max_longitude'],$dist['min_longitude']), 'compare' => 'BETWEEN');

There is no error and there is no results returned, when I run queries via PHPMyAdmin I do get results.

We then run the query given the above arguments;

$args = array('post_type' => 'choirs', 'order' => 'asc', 'orderby' => 'title', 'posts_per_page' => 500,
'meta_query' => array(
$location_LAT,
$location_LAT2,
$location_LNG,
$location_LNG2
));

query_posts($args);

Any help would be appreciated Thanks, P

  • There is no actual query in your code, and only partial argument sets for one. – s_ha_dum Oct 4 '13 at 9:09
  • Sorry sort of left it out as its not really the important part, the query works fine without the meta_query inside it. Have added it in. – paulOr Oct 4 '13 at 9:16
  • Switch from using query_posts to WP_Query instead. That aside, what is the result of your query when you pass it into var_dump($query)? Also do you have your debug constant defined in your wp-config file? – Adam Oct 5 '13 at 13:07
1

In both examples, you are searching between 'max' and 'min', where it should be between 'min' and 'max':

array( $dist['max_latitude'], $dist['min_latitude'] )

'value' => $dist['max_latitude'], 'compare' => '>=');
'value' => $dist['min_latitude'], 'compare' => '<=');

Both mean:

"Search for a value that ist greater than max_latitude and less than min_latitude"

Another finding why my own meta_query didn't work with geo coordinates: 'type' => 'numeric' does an SQL CAST() to SIGNED. This removes all the decimals of the geo coordinates before comparison!

  • 1
    Thanks for sharing that info. Hint: If you ain't got a real solution, you can check the "community wiki" check box when answering. It's then only meant to share knowledge, but don't give or take reputation points when up/downvoting. – kaiser May 26 '14 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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