2015-11-08 200 views
0

製作的wooCommerce產品搜索功能和監控功能get_posts(的$ args = NULL)是如何工作的,我看到設置ARGS像產品過濾功能

 $args = array(
      's'     => 'Some content', 
      'post_type'   => 'product', 
      'post_status'   => 'publish', 
      'ignore_sticky_posts' => 1, 
      'numberposts' => 999999, // set maximim value 
      ... 
     ); 

     $products_list = get_posts($args); 

1)參數「S」讓搜索在兩個post_content和post_title字段。如果有一種方法僅在這些字段中的一箇中進行搜索?

2)我發現設置'category'參數給我需要的類別 - 它根本不起作用。 經過一些測試後,我發現在get_posts函數內部註釋'cat'參數會使'category'參數起作用。 '貓'參數的用途是什麼?

3)在我的搜索中,我對價格,in_stock字段進行過濾。爲了實現這一點,我必須手動檢查這2個標準。我的意思是我必須設置參數

「numberposts」 => 999999,//設置maximim價值爲一些大的數量

,並在循環檢查這些參數,並跳過它們。我不能將這些參數設置爲get_posts函數的過濾器?

如果我可以用get_posts做出我需要的東西,或者如果有更好的工具,也許有一些wooCommerce函數?

4)此外,我做搜索按SKU在argumants加入:

  'meta_value'     => '_sku', 

看起來,這個新的條件下工作的AND,但我需要這項工作作爲OR條件。 如果有辦法做到這一點?

謝謝!

回答

0

元查詢陣列可以採取「關係」,默認爲「AND」的關鍵:

$args = array(
     's'     => 'Some content', 
     'post_type'   => 'product', 
     'post_status'   => 'publish', 
     'ignore_sticky_posts' => 1, 
     'numberposts'   => -1, 
     'meta_query'   => array(
       'relation' => 'OR', 
        array(
        'key' => '_sku', 
        'value' => 'some value', 
        'compare' => '=' 
       ), 
       array(
        'key'  => '_sku', 
        'value' => 'another value', 
        'compare' => '=' 
       ) 
     ); 
    ); 

ALO,numberposts可以採取-1的值返回所有職位。