WooCommerce 使用 ACF 和产品类别过滤产品
我正在使用WooCommerce显示产品循环,并为同一循环构建了过滤器。该过滤器使用ACF元数据,该值直接从查询字符串中拉出。 meta_query正在按预期工作,但是Tax_Query似乎并不无效。我需要一些帮助,说明为什么我的tax_query没有产生正确的结果。
function custom_pre_get_posts_query( $q ) {
$selected_seats = array();
if (isset($_GET['seats']) && $_GET['seats'] != 'all') {
$selected_seats = $_GET['seats'];
}
$selected_event_type = array();
if (isset($_GET['event_type']) && $_GET['event_type'] != 'event_all') {
$selected_event_type = $_GET['event_type'];
}
$selected_location = array();
if (isset($_GET['location']) && $_GET['location'] != 'location_all') {
$selected_location = $_GET['location'];
}
$tax_query = (array) $q->get( 'meta_query' );
$tax_query[] = array(
'tax_query' => array(
'taxonomy' => 'product_category',
'field' => 'slug',
'operator' => 'IN',
'terms' => $selected_location
//terms' => array( $selected_location )
),
'meta_query' => array(
'relation' => 'AND',
// Select in ACF, doesn't need to do array cause only one filter will apply at one time
array(
'key' => 'seats',
'value' => $selected_seats,
'compare' => 'IN',
),
// Checkbox in ACF, doesn't need to do array cause only one filter will apply at one time
array(
'key' => 'event_type',
'value' => $selected_event_type,
'compare' => 'LIKE',
),
array(
'key' => 'car_categories',
'value' => $selected_category,
'compare' => 'LIKE',
),
),
);
// Special query for category
$selected_category = array();
if (isset($_GET['category'])) {
$selected_category = $_GET['category'];
}
$selected_category_query= array(
'relation' => 'OR',
);
foreach($selected_category as $category){
array_push($selected_category_query,array(
'key' => 'car_categories',
'value' =>$category,
'compare' => 'LIKE',
));
}
$tax_query[] = array(
'meta_query' => $selected_category_query,
);
$q->set( 'meta_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
$ tax_query的var_dump导致:
array(2) {
[0]=>
array(2) {
["tax_query"]=>
array(4) {
["taxonomy"]=>
string(11) "product_cat"
["field"]=>
string(2) "id"
["terms"]=>
array(1) {
[0]=>
int(75)
}
["operator"]=>
string(2) "IN"
}
["meta_query"]=>
array(4) {
["relation"]=>
string(3) "AND"
[0]=>
array(3) {
["key"]=>
string(5) "seats"
["value"]=>
array(0) {
}
["compare"]=>
string(2) "IN"
}
[1]=>
array(3) {
["key"]=>
string(10) "event_type"
["value"]=>
array(0) {
}
["compare"]=>
string(4) "LIKE"
}
[2]=>
array(3) {
["key"]=>
string(14) "car_categories"
["value"]=>
NULL
["compare"]=>
string(4) "LIKE"
}
}
}
[1]=>
array(1) {
["meta_query"]=>
array(1) {
["relation"]=>
string(2) "OR"
}
}
}
I am using WooCommerce to display a product loop and have built out a filter for the same loop. The filter uses acf meta which the values are pulled directly from the query string.
The meta_query is working as expected, however the tax_query seems to be not ineffective. I need some assistance as to why my tax_query is not producing the correct results.
function custom_pre_get_posts_query( $q ) {
$selected_seats = array();
if (isset($_GET['seats']) && $_GET['seats'] != 'all') {
$selected_seats = $_GET['seats'];
}
$selected_event_type = array();
if (isset($_GET['event_type']) && $_GET['event_type'] != 'event_all') {
$selected_event_type = $_GET['event_type'];
}
$selected_location = array();
if (isset($_GET['location']) && $_GET['location'] != 'location_all') {
$selected_location = $_GET['location'];
}
$tax_query = (array) $q->get( 'meta_query' );
$tax_query[] = array(
'tax_query' => array(
'taxonomy' => 'product_category',
'field' => 'slug',
'operator' => 'IN',
'terms' => $selected_location
//terms' => array( $selected_location )
),
'meta_query' => array(
'relation' => 'AND',
// Select in ACF, doesn't need to do array cause only one filter will apply at one time
array(
'key' => 'seats',
'value' => $selected_seats,
'compare' => 'IN',
),
// Checkbox in ACF, doesn't need to do array cause only one filter will apply at one time
array(
'key' => 'event_type',
'value' => $selected_event_type,
'compare' => 'LIKE',
),
array(
'key' => 'car_categories',
'value' => $selected_category,
'compare' => 'LIKE',
),
),
);
// Special query for category
$selected_category = array();
if (isset($_GET['category'])) {
$selected_category = $_GET['category'];
}
$selected_category_query= array(
'relation' => 'OR',
);
foreach($selected_category as $category){
array_push($selected_category_query,array(
'key' => 'car_categories',
'value' =>$category,
'compare' => 'LIKE',
));
}
$tax_query[] = array(
'meta_query' => $selected_category_query,
);
$q->set( 'meta_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
a var_dump of $tax_query results in:
array(2) {
[0]=>
array(2) {
["tax_query"]=>
array(4) {
["taxonomy"]=>
string(11) "product_cat"
["field"]=>
string(2) "id"
["terms"]=>
array(1) {
[0]=>
int(75)
}
["operator"]=>
string(2) "IN"
}
["meta_query"]=>
array(4) {
["relation"]=>
string(3) "AND"
[0]=>
array(3) {
["key"]=>
string(5) "seats"
["value"]=>
array(0) {
}
["compare"]=>
string(2) "IN"
}
[1]=>
array(3) {
["key"]=>
string(10) "event_type"
["value"]=>
array(0) {
}
["compare"]=>
string(4) "LIKE"
}
[2]=>
array(3) {
["key"]=>
string(14) "car_categories"
["value"]=>
NULL
["compare"]=>
string(4) "LIKE"
}
}
}
[1]=>
array(1) {
["meta_query"]=>
array(1) {
["relation"]=>
string(2) "OR"
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想在这里尽可能清楚地说明:
您将在不同的键中设置 $meta_query_args 和 $tax_query_args :
您的 meta_query 将设置为:
您的tax_query args将设置为:(根据您的需要进行相应修改。)
您的最终结果对于meta_query 和tax_query 键,查询应该或多或少像这样:
PD:我还没有测试过它,但应该适合您想要实现的目标。
I wanna try to be clear as possible here:
You are gonna set your $meta_query_args and your $tax_query_args in different keys:
Your meta_query will be set like:
Your tax_query args will be set like: (Modify accordingly to your needs.)
your final query should look like this more or less for the meta_query and tax_query keys:
PD: I have not tested it, but should be good for what you want to achieve.