0

I am working on a project for myself whereby I am trying to search users within the WordPress database based on postcode/zip and radius but the code which I'm using takes forever to load and results in a timeout error even though there are only about 200 users. I am quite stumped by this so I am hoping someone out there can lead me in the right direction. Here is the code I have currently:

                $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($_GET['postcode'])."&sensor=false&key=MY-API-KEY";
            $result_string = file_get_contents($url);
            $result = json_decode($result_string, true);

            if(!empty($result['results'])){
                $orig_lat = $result['results'][0]['geometry']['location']['lat'];
                $orig_lon = $result['results'][0]['geometry']['location']['lng'];

            }


            $center_lat = $orig_lat; //insert the lat of where you are
            $center_lng = $orig_lon; //insert the lng of where you are
            $radius = $_SESSION['postcode_radius']; //insert the radius of KM that you want to search

            $multiplier=3959; //miles
            $multiplier=($multiplier*1.609344); //use km instead

            $users = get_users( array( 'fields' => array( 'ID' ) ) );

            //now we got all our users that have latitude (i assume they also have longitude ^^)
            $nearbyusers = array();
            foreach($users as $user){
                      $lat = get_user_meta($user->ID,'lat',TRUE); //assuming the user latitude is a meta field named "latitude"
                      $lng = get_user_meta($user->ID,'lng',TRUE); //assuming the user longitude is a meta field named "longitude"
                      $distance = ( $multiplier * acos( cos( deg2rad($center_lat) ) * cos( deg2rad( $lat ) ) * cos( deg2rad( $lng ) - deg2rad($center_lng) ) + sin( deg2rad($center_lat) ) * sin( deg2rad( $lat ) ) ) );
                      if($distance<$radius) {
                          $nearbyusers[] = $user;
                      }
            }
            print_r($nearbyusers);

Any ideas on this and why it doesn't work as expected?

Thanks!

  • Have you considered instead of making an API call on each, that you add some user_meta for lat, lng and then do your radius search on a simple query. Either add the user_lat, user_lng on registration, or with some cron task. – Howard E Apr 7 at 16:30
  • I actually am storing the lat and lng within the database. The API call I am using is taking the postcode from the search results page. I am sure that is the right thing to do and base it on that? – mcmaster.1986 Apr 8 at 8:31
  • Are you storing the lat,lng in the user_meta? If so, what is the meta field for that? – Howard E Apr 8 at 9:37
  • Thanks Howard but I've managed to resolve it. The issue was with get_users(). It just kept timing out for some reason. I fixed it with a custom sql query: $users = $wpdb->get_results("SELECT DISTINCT ID FROM wp_users"); – mcmaster.1986 Apr 8 at 9:39
  • You could do it even faster by doing your select from users and right join the user meta lat,lng. WHERE distance (using haversine) etc. – Howard E Apr 8 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.