Я проверяю расстояние между почтовыми индексами в запросе WordPress.
Пользователь ищет почтовый индекс, и мы l oop просматриваем все сообщения, чтобы вычислить расстояние между отправленным почтовым индексом и полем почтового индекса сообщений (для этого используются дополнительные настраиваемые поля).
I около 100 строк, и загрузка занимает около 10 секунд.
<?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[0]->elements[0]->distance->value / 1000 * 0.62137, 1);
//echo the distance
echo $distance;
endwhile; endif;
} ?>
Есть ли способ ускорить процесс, или он выполняется так много раз, что будет настолько медленным?