I am trying to figure the best way to do add a distance radius to an existing query. I have a posts_where function that works on its own, but it does not work with and meta_query or tax_query vars
Current query:
$myposts = new WP_Query( array(
'post_status' => 'publish',
'post_type' => 'guides',
'meta_query' => $meta_query,
'tax_query' => $tax_query
)
);
The meta and tax queries look like this:
$tax_query = array('relation' => 'AND');
if (isset($waters)){
$tax_query[] = array(
'taxonomy' => 'water',
'field' => 'id',
'terms' => $waters
);
}
if (isset($fish)){
$tax_query[] = array(
'taxonomy' => 'fish',
'field' => 'id',
'terms' => $fish
);
}
if (isset($fish) && isset ($waters)){
$tax_query[0] =
array(
'taxonomy' => 'fish',
'field' => 'id',
'terms' => $fish
);
$tax_query[1] =
array(
'taxonomy' => 'water',
'field' => 'id',
'terms' => $waters
);
}
Then the current where filter is this:
function my_geo_where( $where ) {
global $wpdb;
$dist = $_GET["dist"];
$where .= " HAVING distance < '$dist'";
return $where;
}
I tried AND instead of HAVING but it didn't work
This is the request for a distance radius:
SELECT SQL_CALC_FOUND_ROWS wp_posts.*, pm1.meta_value as lat, pm2.meta_value as lon, ACOS(SIN(RADIANS(25.1837127))*SIN(RADIANS(pm1.meta_value))+COS(RADIANS(25.1837127))*COS(RADIANS(pm1.meta_value))*COS(RADIANS(pm2.meta_value)-RADIANS(-80.3881333))) * 3959 AS distance FROM wp_posts INNER JOIN wp_postmeta pm1 ON wp_posts.id = pm1.post_id AND pm1.meta_key = 'wpcf-lat'
INNER JOIN wp_postmeta pm2 ON wp_posts.id = pm2.post_id AND pm2.meta_key = 'wpcf-long' WHERE 1=1 AND wp_posts.post_type = 'guides' AND (wp_posts.post_status = 'publish') HAVING distance < '50' ORDER BY wp_posts.post_date DESC LIMIT 0, 10
This is the request for a distance radius and a meta query:
SELECT SQL_CALC_FOUND_ROWS wp_posts.*, pm1.meta_value as lat, pm2.meta_value as lon, ACOS(SIN(RADIANS(25.1837127))*SIN(RADIANS(pm1.meta_value))+COS(RADIANS(25.1837127))*COS(RADIANS(pm1.meta_value))*COS(RADIANS(pm2.meta_value)-RADIANS(-80.3881333))) * 3959 AS distance FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) INNER JOIN wp_postmeta pm1 ON wp_posts.id = pm1.post_id AND pm1.meta_key = 'wpcf-lat'
INNER JOIN wp_postmeta pm2 ON wp_posts.id = pm2.post_id AND pm2.meta_key = 'wpcf-long' WHERE 1=1 AND wp_posts.post_type = 'guides' AND (wp_posts.post_status = 'publish') AND ( (wp_postmeta.meta_key = 'wpcf-guests' AND CAST(wp_postmeta.meta_value AS CHAR) >= '5') ) HAVING distance < '50' GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10
Abdelhadi Touil answers:
Hi.
Have enabled the Debug mode to see what happen? I'm not an expert in Mysql, but hope this link help you:
[[LINK href="http://stackoverflow.com/questions/9453652/using-where-clause-to-find-poi-within-a-range-of-distance-from-longitude-and-lat"]]http://stackoverflow.com/questions/9453652/using-where-clause-to-find-poi-within-a-range-of-distance-from-longitude-and-lat[[/LINK]]
I have just seen your question with no answer, so I'd like to give some help, and hope you good luck :)
Kyle comments:
Thanks for the reply
I have been scouring stackoverflow, but didn't come across that one. They tried a little something different so I will give that one a shot :)
Kyle comments:
And to answer your question, yes the two requests are my attempts at debugging
Kyle comments:
No luck, thanks though
Dbranes answers:
Hi Kyle, are the two SQL queries working?
Are you looking for a SQL query that contains tax+meta+distance ?