代码之家  ›  专栏  ›  技术社区  ›  Nate Beers

当数据太多时,元查询不返回任何内容

  •  1
  • Nate Beers  · 技术社区  · 6 年前

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

    在我拿到几千个邮政编码之前,一切都很顺利。

    我正在将一系列的邮政编码传递给 meta_query 并对照我的自定义邮件类型检查 dealer 用它 key 属于 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 );
    
      ?>
    

    在那之后,我只是做标准 loop HTML 100 miles ,对于美国东北部的一些邮政编码,它返回的邮政编码超过2500个。

    例如,75英里处的19804返回大约1200个邮政编码,并正确显示9个经销商。在100英里处19804返回大约2200个邮政编码,显示循环点击 else 并说没有一家经销商与这些数据相符。

    在其他只返回大约1000个邮政编码的100英里处返回数据并正确显示。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Nate Beers    6 年前

    找到了问题。我查了一下WordPress debug.log 和锯子 Query Killed WP Engine 托管时,它们限制特定大小的查询。

    所以,在 wp-config define( 'WPE_GOVERNOR', false ); 这降低了WP引擎的查询限制。

    推荐文章