The code below I have written to do a radius search within 50 miles of the searched zip code. How can I change this code to find only the nearest clinic location, instead of a list of clinic locations? So if I typed in a zip code for Denver, Colorado and my locations are mainly in New York, PA, and Ohio, how can I get the nearest location located in Ohio?
/* RADIUS SEARCH - 50 MILES FROM THE ZIP CODE VARIABLE */
$zipcode=trim($_REQUEST['zipcode']);
//Find Out The Longitude & Latitude of Zipcode in existing database
$zcquery = "SELECT * FROM wp_nava5jxxyp_zipcodes WHERE zipcode = $zipcode";
$result = mysqli_query($dbc, $zcquery);
$row = mysqli_fetch_array($result);
$lat = $row['latitude'];
$long = $row['longitude'];
//Setting the Default Distance 50 Miles
// (Equation 1 / 69 = 0.0144927536231884 * 50 miles equals result below)
$miles = 0.7246376811594203;
/* Query To Search for all of the Zip Codes within the range */
$query = 'SELECT zipcode from wp_nava5jxxyp_zipcodes
WHERE latitude between ' . $lat . ' - ' . $miles . ' and ' . $lat . ' + ' . $miles . '
and longitude between ' . $long . ' - ' . $miles . ' and ' . $long . ' + ' . $miles;
$result = mysqli_query($dbc, $query);
//Put those zip codes into a variable array
$variabele = array();
while($row = mysqli_fetch_array($result))
{
if($row['zipcode'] != '')
$variabele[] = $row['zipcode'];
}
$zipcodelist = implode(', ', $variabele);
// Close Connection
mysqli_close($dbc);
//Query Database For Any Clinics that are included in zip code list
$args = array(
'posts_per_page' => 10,
'orderby' => 'post_title',
'order' => 'DESC',
'post_type' => 'clinics',
'meta_query' => array (
array (
'key' => 'cliniczipcode',
'value' => $zipcodelist,
'compare' => 'IN'
)
) );
// the query
$the_query = new WP_Query( $args );