【问题标题】:Meta Query Returns Nothing when Too Much Data数据过多时元查询不返回任何内容
【发布时间】:2018-12-20 17:33:52
【问题描述】:

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

在我获得几千个邮政编码之前,它似乎工作正常。

我将邮政编码数组传递给meta_query,并对照我的自定义帖子类型dealer 及其keyzip 进行检查,以查找所有邮政编码包含在$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 个经销商。 19804 在 100 英里处返回大约 2200 个邮政编码,显示循环点击 else 并显示没有经销商匹配该数据。

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

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

【问题讨论】:

    标签: mysql wordpress meta-query


    【解决方案1】:

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

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

    【讨论】:

    • 出于好奇,“特定大小的查询”是什么意思——因为查询字符串太长了?还是查询结果太大? WP Engine 这样做可能是有原因的(性能),并且覆盖它也可能会产生后果,因此您可能需要考虑另一种方法。 WP_Query 与简单的 $wpdb-&gt;get_results("SELECT post_id FROM {$wpdb-&gt;post_meta} WHERE....") 相比“重”,因此您可能需要考虑重构为“轻量级”查询以保持您的网站性能。
    猜你喜欢
    • 2012-04-14
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2013-09-10
    • 1970-01-01
    相关资源
    最近更新 更多