2016-12-16 111 views
0

我显然没有这样做。任何帮助将不胜感激。本质上,我拉附近的邮政编码和城市名称正在进行的搜索。然后我想使用WP_Query来拉取与这些邮政编码和城市名称相匹配的WordPress邮政类型。我不是超级熟悉的WordPress,所以极有可能我是如何做到这一点大错特错:将多个参数传递给WP_Query

$url = "http://www.zipcodeapi.com/rest/OeAp3k78myEhBy0oqSlQSlUWOt6N7TjW8Tlbdtkz1YRCwS1WKmNDIHzwbFjizCeI/radius.json/" . $searchbox . "/100/km"; 
$response = file_get_contents($url); 
$json = json_decode($response); 

$post_type = 'location'; 
$citysearcharray = array(); 
$zipsearcharray = array(); 

$searcharray = array(); 
foreach($json->zip_codes as $nearbyzip) 
{ 

    $citysearcharray[] = array(
     'key'  => 'city', 
     'value'  => $nearbyzip->city, 
     'compare' => '=' 
    ); 

    $zipsearcharray[] = array(
     'key'  => 'zip_code', 
     'value'  => $nearbyzip->zip_code, 
     'compare' => '=' 
    ); 
} 

      $args = array(
      'post_type'  => $post_type, 
      'post_status'  => 'publish', 
      'caller_get_posts'=> 1, 
      'posts_per_page' => 10, 
      'meta_query' => array(
       'relation'  => 'OR', 
      $citysearcharray, 
      $zipsearcharray)); 

    $my_query = null; 
    $my_query = new WP_Query($args); 

    if($my_query->have_posts()) { 
     $count=0; 


     echo '<ul class="location">'; 
      while ($my_query->have_posts()) : $my_query->the_post(); 
... 

艾米我一起来正确地把的$ args?的,而不是添加新的完整的值传送到阵列

TIA

回答

1

在你的foreach,

$zipsearcharray 

$citysearcharray 

具有值密钥作为阵列,使得:

'value' => array(array of cities)

然后改变

compare => 'IN' 
+0

喜欢这个? \t的foreach($ json-> zip_codes作为nearbyzip $) \t { \t \t $ citysearcharray [] =阵列($ nearbyzip->地点); \t \t $ zipsearcharray [] =数组($ nearzip-> zip_code); \t} \t的$ args =阵列( \t 'post_type'=> $ post_type, \t 'post_status'=> '发布', \t 'caller_get_posts'=> 1, \t 'posts_per_page'=> 10, \t 'meta_query' \t =>数组( \t \t '关系' \t \t => 'OR', \t阵列( \t \t \t '钥匙' \t \t => '城市', \t \t \t '值' \t \t => $ citysearcharray, \t \t \t '比较' \t => 'IN' \t \t) \t阵列( \t \t \t“键'\t \t => 'ZIP_CODE', \t \t \t '值' \t \t => $ zipsearcharray, \t \t \t'比较'\t =>'IN' \t \t))); – Nugs

+0

是的,这是否适合你? – baku

+0

我得到:mysqli_real_escape_string()期望参数2是字符串,数组在/ nas/content/staging/agemarkch/wp-includes/wp-db中给出。php on line 1166 – Nugs