2017-08-31 18 views
-1

中獲取任意值的信息您好,我正在嘗試做這個查詢,提取具有3個屬性(Collezione,Finitura,Pietre)的woocommerce產品。但是我們可以沒有設置1,2或3個過濾器,所以我想問你,如果過濾器沒有設置,我必須設置哪個值。我以爲-1但這不在這裏工作。WP在屬性

下面你可以找到我的代碼。提前致謝。

$args = array(
    'post_type' => 'product', 
    'post_status' => 'publish', 
    'posts_per_page' => -1, 
    'tax_query'    => array(
     'relation' => 'AND', 
     array(
      'taxonomy'  => 'product_cat', 
      'field' => 'term_id', 
      'terms'   => $cat_id, 
      'operator'  => 'IN' 
     ), 
     array(
      'taxonomy'  => 'pa_collezione', 
      'field' => 'term_taxonomy_id', 
      'terms'   => $pa_collezione, 
      'operator'  => 'IN' 
     ), 
     array(
      'taxonomy'  => 'pa_finitura', 
      'field' => 'term_taxonomy_id', 
      'terms'   => $pa_finitura, 
      'operator'  => 'IN' 
     ),    
     array(
      'taxonomy'  => 'pa_pietre', 
      'field' => 'term_taxonomy_id', 
      'terms'   => $pa_pietre, 
      'operator'  => 'IN' 
     ) 
    ) 
); 

$products = get_posts($args); 

回答

0

我覺得你的條件,如 'pa_collezione' 是你在你的product_cat分類。

您的tax_query應該只爲條款字段提供一組條款。

'tax_query' => array(
     array(
      'taxonomy' => 'product_cat', 
      'field' => 'term_id', 
      'terms' => $array_of_term_ids, 
     ), 
    ) 

你在你的條件數組中做你的過濾器,而不是增加額外的關係。

+0

我找到了解決方案。如果category是'-1',我已經做了一個檢查'NOT IN'。現在我粘貼解決方案。感謝您的幫助和時間。 –

+0

你可以這樣做。但是,在pa_finitura中是否有術語,或者pa_finitura是product_cat中的術語?只要知道何時創建術語和分類。 – TurtleTread

+0

它是taxonomy_term_id .. pa_finitura是正確的 –

0

如果未設置參數,我找到了解決方案設置'NOT IN'而不是'IN'。 下面的解決方案。

$cat_id = $_POST['category']; 
$pa_finitura= $_POST['finitura']; 
$pa_collezione= $_POST['collection']; 
$pa_pietre= $_POST['pietre']; 

if($pa_collezione=="-1"){$cond_collezione='NOT IN';}else{$cond_collezione = 'IN';} 
if($pa_finitura=="-1"){$cond_finitura='NOT IN';}else{$cond_finitura = 'IN';} 
if($pa_pietre=="-1"){$cond_pietre='NOT IN';}else{$cond_pietre = 'IN';} 

define('WP_USE_THEMES', false); 
require_once('../../../../wp-load.php'); 
$args = array(
    'post_type' => 'product', 
    'post_status' => 'publish', 
    'posts_per_page' => -1, 
    'tax_query'    => array(
     'relation' => 'AND', 
     array(
      'taxonomy'  => 'product_cat', 
      'field' => 'term_id', //This is optional, as it defaults to 'term_id' 
      'terms'   => $cat_id, 
      'operator'  => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. 
     ), 
     array(
      'taxonomy'  => 'pa_collezione', 
      'field' => 'term_taxonomy_id', //This is optional, as it defaults to 'term_id' 
      'terms'   => $pa_collezione, 
      'operator'  => $cond_collezione // Possible values are 'IN', 'NOT IN', 'AND'. 
     ), 

     array(
      'taxonomy'  => 'pa_finitura', 
      'field' => 'term_taxonomy_id', //This is optional, as it defaults to 'term_id' 
      'terms'   => $pa_finitura, 
      'operator'  => $cond_finitura // Possible values are 'IN', 'NOT IN', 'AND'. 
     ), 


     array(
      'taxonomy'  => 'pa_pietre', 
      'field' => 'term_taxonomy_id', //This is optional, as it defaults to 'term_id' 
      'terms'   => $pa_pietre, 
      'operator'  => $cond_pietre // Possible values are 'IN', 'NOT IN', 'AND'. 
     ) 
    ) 
); 
$products = get_posts($args);