2017-08-31 10 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

解決策が見つかりました。カテゴリが '-1'の場合は 'NOT IN'を入れてチェックしました。今私は解決策を貼り付けます。助けていただきありがとうございます。 –

+0

あなたはそうすることができます。しかし、pa_finituraには用語がありますか、product_catにはpa_finituraという用語がありますか?用語とタクソノミーを作成する時期を知るだけです。 – TurtleTread

+0

taxonomy_term_id .. pa_finituraが正しい –

0

私は、パラメータが設定されていない場合、 'IN'の代わりに 'NOT 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); 
関連する問題