Home Forums Front-end Issues WP Query closest locations by ACF Google Map values Reply To: WP Query closest locations by ACF Google Map values

  • In the case of finding a location close to a variable location then you’d need a lookup table that stores the longitude and latitude for each post then you can loop over this table to calculate distances for each and find the ones you want. You’d still need to loop over every location but you would not need to retrieve every post to do it which would be much faster. You could also store the results in a transient so you did not need to do the same lookup if it was already done.

    $locations = array(
      $post_id => array('lon' => $lon, 'lat' => $lat)

    In this case you would not really need a cron as you could just update the values for each post when saved, but it would mean that you’d also probably want to remove them when a post is deleted to prevent the table from being larger than necessary. Possible delete any transients if you decide to use them as well.