Do you mean that you use the Google Maps Geocoding Service to Geocode the instructors addresses on every pageload? That is not a wise thing to do because of two reasons:
- Performance. If you have to geocode every address every search, that would very slow.
- Cost. Googles APIs are only free below a specific use each month.
The better way would be to geocode the instructors addresses on Post Type Save, save the latitude and longitude in the database, and on searching for zip use the Geocoding Feature for the zip to make a custom query based on the latitude and longitude of the searcher.
As i don't know how exactly you save the addresses, i can't help you there. But i can give you a function to get the posts that are in a specific radius from your users location: (this presumes that your post type key is "instructor" and the latitude and longitude of their addresses are stored in post meta keys "lat" and "lng")
function get_posts_in_radius($center_lat,$center_lng,$radius_in_km=50){
$multiplier=3959; //miles
$multiplier=($multiplier*1.609344); //use km instead
$sql = $wpdb->prepare("SELECT $wpdb->posts.ID, pm1.post_id, pm1.meta_value AS lat,
pm2.meta_value AS lng,
(%f * ACOS( COS( RADIANS(%f) ) * COS( RADIANS( pm1.meta_value ) ) * COS( RADIANS( pm2.meta_value ) - RADIANS(%f) ) + SIN( RADIANS(%f) ) * SIN( RADIANS( pm1.meta_value ) ) ) ) AS distance
FROM $wpdb->posts
INNER JOIN $wpdb->postmeta pm1
ON $wpdb->posts.ID = pm1.post_id
INNER JOIN $wpdb->postmeta pm2
ON pm1.post_id = pm2.post_id
AND pm1.meta_key = 'lat'
AND pm2.meta_key = 'lng'
WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'instructor' HAVING distance < %d ORDER BY distance ASC",$multiplier,$center_lat,$center_lng,$center_lat,$radius_in_km);
$response = array();
if($rows = $wpdb->get_results($sql,ARRAY_A)){
foreach($rows as $row){
$response[] = array(
'id' => $row['post_id'],
'title' => get_the_title($row['post_id']),
'lat' => $row['lat'],
'lng' => $row['lng'],
'distance' => $row['distance']
);
}
}
return $response;
}
Happy Coding!