2017-08-31 1 views
-1

Hallo, ich versuche diese Abfrage, die Woocommerce-Produkte mit 3 Attributen (Collezione, Finitura, Pietre) zu extrahieren. Aber wir können keine, 1,2 oder 3 Filter haben, also möchte ich Sie fragen, welchen Wert ich einstellen muss, wenn der Filter nicht gesetzt ist. Ich dachte -1 aber das funktioniert hier nicht.WP Erhalten Sie Beiträge mit einem beliebigen Wert in Attribut

Hier unten finden Sie meinen Code. Danke im Voraus.

$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); 

Antwort

0

Ich denke, Ihre Begriffe wie 'pa_collezione' Ihr in Ihrer product_cat Taxonomie sind.

Ihre tax_query sollte nur eine Reihe von Begriffen für das Feld "terms" enthalten.

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

Ihre tun, um Ihre Filter in Ihren Bedingungen Array, nicht durch zusätzliche Beziehungen hinzufügen.

+0

Ich fand die Lösung. Ich habe einen Check gemacht und 'NOT IN' gesetzt, wenn die Kategorie '-1' ist. Jetzt klebe ich Lösung. Danke für Ihre Hilfe und für Ihre Zeit. –

+0

Sie können es so sicher machen. Aber gibt es Begriffe in pa_finitura oder ist pa_finitura ein Begriff in product_cat? Wissen Sie, wann Sie Begriffe und Taxonomie erstellen. – TurtleTread

+0

es ist taxonomy_term_id .. pa_finitura ist richtig –

0

Ich habe eine Lösung Einstellung "NOT IN" statt "IN" gefunden, wenn der Parameter nicht festgelegt ist. Hier unten die Lösung.

$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); 
Verwandte Themen