2014-01-30 86 views
0

對,我有read through the entire wpdbezSQL所以我對wpdb類可以做什麼有非常好的理解......實質上wpdb是SQL對於jQuery來說JavaScript是什麼!因爲我以前從來沒有這樣做過,所以我不會對SQL嘗試不太熟悉,但這就是我今天所做的顯示我已經嘗試過的事情,只是後來才知道AND和OR不能是在WP_Query中使用:\AND和或在Wordpress中查詢wpdb

最終結果應該是,如果用戶從ProductType下拉框中選擇並單擊搜索按鈕,頁面應該從選擇菜單中返回基於該術語的結果。如果他們從ProductGroup中選擇一個選項並點擊搜索按鈕,則結果需要根據選擇返回。

如果這兩個下拉菜單都選擇了選項,則需要查詢ProductType和ProductGroup並返回結果。

$args = array(

     'post_type' => 'product', 
     'posts_per_page' => -1, 
     'orderby' => 'title', 
     'order' => 'ASC', 
     'relation'=>'AND', // or OR 
     'relation'=>'OR', // this is naturally not correct 
     'meta_query' => array(
       array(
        'key'  => 'product_type', 
        'value'  => $ProductType, 
        'compare' => 'LIKE' 
       ), 
       array(
        'key'  => 'product_group', 
        'value'  => $ProductGroup, 
        'compare' => 'LIKE' 
       ) 
      ) 

    ); 

     <form name="x" action="" method="GET" class="search-results-form" > 
      <select name="productType" class="search-product-type"> 
       <option value="">Product Type</option> 
       <option value="ProductType1">ProductType1</option> 
       <option value="ProductType2">ProductType2</option> 
      </select> 
      <select name="productGroup" class="search-product-type"> 
       <option value="">Product Type</option> 
       <option value="ProductGroup1">ProductGroup1</option> 
       <option value="ProductGroup2">ProductGroup2</option> 
      </select> 
      <input type="submit" value="SEARCH" class="submit-button btn" /> 
     </form> 

我希望這是有道理的,所以如果有人能給我一個出發點,我會很感激。

回答

0

您可以使用關聯性條件是這樣的: -

$args = array(
    'post_type' => 'product', 
    'posts_per_page' => -1, 
    'orderby' => 'title', 
    'order' => 'ASC', 
    'meta_query' => array(
     'relation'=>'AND', // or OR 
     array(
      'key'  => 'product_type', 
      'value'  => $ProductType, 
      'compare' => 'LIKE' 
     ), 
     array(
      'key'  => 'product_group', 
      'value'  => $ProductGroup, 
      'compare' => 'LIKE' 
     ) 
    ) 
); 

而且你還可以根據您的需要改變relation ...

+0

謝謝Akshay ...我看我應該有不同的標題我的網頁。我需要同時使用AND和OR,這顯然是不允許的,因此移動使​​用wpdb編寫SQL查詢 – SixfootJames

0

我知道這是一個老問題,但萬一有人絆倒(像我一樣),這應該工作:

<?php 
    $args = array(
     'post_type' => 'product', 
     'posts_per_page' => -1, 
     'orderby' => 'title', 
     'order' => 'ASC', 
     // 'relation'=>'AND', // Not required, as it defaults to "AND" 
     'meta_query' => array() 
    ); 

    if (isset($_GET['product_type']) && !empty($_GET['product_type'])) { 
     $args['meta_query'][] = array(
      'key'  => 'product_type', 
      'value'  => $ProductType, 
      'compare' => 'LIKE' 
     ); 
    } 
    if (isset($_GET['product_group']) && !empty($_GET['product_group'])) { 
     $args['meta_query'][] = array(
      'key'  => 'product_group', 
      'value'  => $productGroup, 
      'compare' => 'LIKE' 
     ); 
    } 
?> 

<form name="x" action="" method="GET" class="search-results-form" > 
    <select name="productType" class="search-product-type"> 
     <option value="">Product Type</option> 
     <option value="ProductType1">ProductType1</option> 
     <option value="ProductType2">ProductType2</option> 
    </select> 
    <select name="productGroup" class="search-product-type"> 
     <option value="">Product Type</option> 
     <option value="ProductGroup1">ProductGroup1</option> 
     <option value="ProductGroup2">ProductGroup2</option> 
    </select> 
    <input type="submit" value="SEARCH" class="submit-button btn" /> 
</form>