2016-12-16 174 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