Site icon Hip-Hop Website Design and Development

Order wp_query by calculated area

Hello I am engaged on a retailer locator.
I’ve a customized submit “clinics” the place I save clinic particulars (handle,.., lat, lng). Every time I publish a brand new clinic I save post_id, lat and lng additionally in a customized desk “lat_lng_post”.

I can search clinics inside a particular distance utilizing filter post_where on this method:

add_filter( 'posts_where' , 'location_posts_where' )


    operate location_posts_where( $the place )
        {
            // $lat  and $ lng are latitude and longitude of the searched level
            world $wpdb;    
            $the place .= " 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 $the place;
                    }

I get the clinics throughout the fastened distance (150km) however not ordered in accordance with the gap. They’re proven in accordance with the submit date.
I attempted to set orderby to none:

$args = array (
    'post_type'              => 'clinica',
    'orderby'                => 'none'

);

However It did not work. How can I power to order by that ORDER BY rule I set within the question?