Is there a way to combine both tax_queries and meta_queries using wp_query->set() ?
I am trying to create an AJAX filter for WooCommerce, and I'm currently building out the conditional wp_query->set() in my ajax_functions.php file.
The final filter I am stuck with is when I am filtering for both a price_range, and multiple tag, checkbox or attributes selected. This is the query I want to run:
if($value['pRange'] && $value['tags']) {
$wp_query->set('tax_query', array(
'relation' => 'OR',
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => $value['tags'],
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $value['tags'],
),
array(
'taxonomy' => 'pa_branding',
'field' => 'slug',
'terms' => $value['tags'],
),
)
);
// I want to include the meta_query in the set() method above.
'meta_query', array(
'relation' => 'AND',
array(
'key' => '_price',
'value' => array($value["pRange"][0], $value["pRange"][1]),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
),
}
Could anyone help me on how I might do this?
Thanks in advance!
Is there a way to combine both tax_queries and meta_queries using wp_query->set() ?
I am trying to create an AJAX filter for WooCommerce, and I'm currently building out the conditional wp_query->set() in my ajax_functions.php file.
The final filter I am stuck with is when I am filtering for both a price_range, and multiple tag, checkbox or attributes selected. This is the query I want to run:
if($value['pRange'] && $value['tags']) {
$wp_query->set('tax_query', array(
'relation' => 'OR',
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => $value['tags'],
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $value['tags'],
),
array(
'taxonomy' => 'pa_branding',
'field' => 'slug',
'terms' => $value['tags'],
),
)
);
// I want to include the meta_query in the set() method above.
'meta_query', array(
'relation' => 'AND',
array(
'key' => '_price',
'value' => array($value["pRange"][0], $value["pRange"][1]),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
),
}
Could anyone help me on how I might do this?
Thanks in advance!
Share Improve this question edited Feb 21, 2020 at 20:37 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Feb 21, 2020 at 20:25 parvez noorparvez noor 1514 bronze badges1 Answer
Reset to default 1Just use another set()
method:
if ($value['pRange'] && $value['tags']) {
$wp_query->set('tax_query', array(
'relation' => 'OR',
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => $value['tags'],
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $value['tags'],
),
array(
'taxonomy' => 'pa_branding',
'field' => 'slug',
'terms' => $value['tags'],
),
)
);
$wp_query->set('meta_query', array(
'relation' => 'AND',
array(
'key' => '_price',
'value' => array($value["pRange"][0], $value["pRange"][1]),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
)
);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744725228a4590135.html
评论列表(0条)