I am checking distance between postcodes in a WordPress Query.
The user searches for a postcode and we loop through all posts to calculate the distance between the submitted postcode and the posts postcode field (using Advanced Custom Fields for this).
I have about 100 rows and it’s taking around 10 seconds to load.
<?php $args = array( 'post_type' => $post_type, 'orderby' => 'slug', 'tax_query' => array( array( 'taxonomy' => $taxonomy_name, 'terms' => $child_term->term_id ) ) ); $wp_query = new WP_Query( $args ); $postID = $wp_query->ID; //start the wordpress loop if (have_posts()) : while (have_posts()) : the_post(); //get postcode custom field from the post $store_postcode = get_field('store_postcode', $postID); //use google distasnce matrix api to get distance between user submitted postcode and the posts postcode field $api = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&sensor=false®ion=GB&gl=gb&origins=".$store_postcode."&destinations=".$user_postcode."&key=MY_API_KEY"); $data = json_decode($api); //calculate the distance in miles $distance = round((int)$data->rows->elements->distance->value / 1000 * 0.62137, 1); //echo the distance echo $distance; endwhile; endif; } ?>
Is there a way to speed up the process, or is running this so many times just going to be that slow?