首页 > 解决方案 > 数据过多时元查询不返回任何内容

问题描述

我正在为一家公司进行“查找经销商”查找,以便用户可以输入邮政编码和半径并找到该半径内的所有经销商。

在我得到几千个邮政编码之前,它似乎工作得很好。

我将邮政编码数组传递给 ameta_query并使用它的 of 对照我的自定义帖子类型检查dealerkeyzip以查找所有具有包含在 .zip 中的邮政编码的经销商$zip_array

  <?php
  // This takes in a zip code and returns all zip codes in a specific radius
  // using the zip code api: https://www.zipcodeapi.com/API#radius
  $api_root   = 'https://www.zipcodeapi.com/rest';
    $api_key    = $ZIP_CODE_API_KEY;
    $zip_radius = isset($_POST['radius']) ? $_POST['radius'] : 25;
    $zip_code   = $_POST['zip'];
    $type       = isset($_POST['type']) ? $_POST['type'] : array('architectural','auto','safety-security');

    $api_url    = $api_root.'/'.$api_key.'/radius.json/'.$zip_code.'/'.$zip_radius.'/miles?minimal';

    $curl = curl_init($api_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $curl_response = curl_exec($curl);
    if ($curl_response === false) {
    $info = curl_getinfo($curl);
    curl_close($curl);
    die('error occured during curl exec. Additional info: ' . var_export($info));
    }

// Because all zip codes come back as strings, we set up an array to push 
// the new zip code integers into
$zip_array = array();

// Decode response for easy looping
$curl_response = json_decode($curl_response);

// var_dump($curl_response);

// Change zip code strings to integers and push into zip_array
foreach ($curl_response as $zipcode) {
    foreach ($zipcode as $the_zip) {
        array_push($zip_array, intval($the_zip));
    }
}

 $meta_query_args = array(
    'post_type'      => 'dealer',
    'posts_per_page' => -1,
    'meta_query'     => array(
        array(
            'key'     => 'zip',
            'value'   => $zip_array,
            'compare' => 'IN',
            'type'    => 'NUMERIC'
        )
    ),
    'tax_query' => array(
        array(
            'taxonomy' => 'types',
            'field'    => 'slug',
            'terms'    => $type
        )
    )
);
$meta_query = new WP_Query( $meta_query_args );

  ?>

在那之后,我只是在做标准loopHTML并在页面上呼应所有经销商信息。API 正在按应有的方式返回所有邮政编码,并且所有内容都按应有的方式显示并快速显示,但是我注意到,当我将半径设置为 时100 miles,对于美国东北部的某些邮政编码,它会返回 2500 以上的邮政编码代码。

例如,75 英里处的 19804 返回大约 1200 个邮政编码并正确显示 9 个经销商。100 英里处的 19804 返回大约 2200 个邮政编码,显示循环点击并显示else没有经销商匹配该数据。

其他仅返回 1000 左右的邮政编码的 100 英里会返回数据并正确显示。

这仅仅是一个必须检查太多邮政编码的情况,还是我走得太远了,这是完全不同的事情?

标签: mysqlwordpressmeta-query

解决方案


发现问题。我检查了 WordPressdebug.log并看到了Query Killed. 谷歌搜索了这个问题,发现在WP Engine托管上,他们限制了一定大小的查询。

因此,在wp-config我添加的行define( 'WPE_GOVERNOR', false );中,这取消了 WP Engine 的查询限制。


推荐阅读