Hi I'm working on a store locator. I have a custom post "clinics" where I save clinic details (address,.., lat, lng). Each time I publish a new clinic I save post_id, lat and lng also in a custom table "lat_lng_post".
I'm able to search clinics within a specific distance using filter post_where in this way:
add_filter( 'posts_where' , 'location_posts_where' )
function location_posts_where( $where )
{
// $lat and $ lng are latitude and longitude of the searched point
global $wpdb;
$where .= " AND $wpdb->posts.ID IN (SELECT post_id FROM lat_lng_post
WHERE
(6371 * acos( cos( radians(" . $lat . ") )
* cos( radians( lat ) )
* cos( radians( lng )
- radians(" . $lng . ") )
+ sin( radians(" . $lat . ") )
* sin( radians( lat ) ) ) ) <= 150
ORDER BY
( 6371 * acos( cos( radians(" . $lat . ") )
* cos( radians( lat ) )
* cos( radians( lng )
- radians(" . $lng . ") )
+ sin( radians(" . $lat . ") )
* sin( radians( lat ) ) ) ) ASC)";
return $where;
}
I get the clinics within the fixed distance (150km) but not ordered according to the distance. They are shown according to the post date. I tried to set orderby to none:
$args = array (
'post_type' => 'clinica',
'orderby' => 'none'
);
But It didn't work. How can I force to order by that ORDER BY rule I set in the query?
posts_orderby
. referenceposts_order_by
for your CPT and this should overwrite any other orderby given by $args. edit where did you get your$lat
resp.$lng
in the code. if they aren't defined, they will return 0;