WooCommerce 使用 ACF 和产品类别过滤产品
WooCommerce Filter products with ACF and Product Category
我正在使用 WooCommerce 来显示产品循环,并为同一循环构建了一个过滤器。过滤器使用 acf meta,其值直接从查询字符串中提取。
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' );
a var_dump of $tax_query 结果:
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"
}
}
}
我想在这里尽可能说清楚:
您要将 $meta_query_args 和 $tax_query_args 设置为不同的键:
您的 meta_query 将设置为:
$meta_query_args = 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',
),
);
$q->set( 'meta_query', $meta_query_args );
您的 tax_query 参数将设置为:(根据您的需要进行相应修改。)
$tax_query_args = array(
'relation' => 'AND', // AND , OR (Do not use with a single inner taxonomy array)
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'action', 'comedy' ),
),
//optionally if you are trying to query more than one taxonomy
array(
'taxonomy' => 'custom_tax_whatever',
'field' => 'term_id',
'terms' => array( 103, 115, 206 ),
),
);
$q->set( 'tax_query', $tax_query_args );
对于 meta_query 和 tax_query 键,您的最终查询应该大致如下所示:
array(
'tax_query' => array(
'relation' => 'AND', // AND , OR (Do not use with a single inner taxonomy array)
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'action', 'comedy' ),
),
//optionally if you are trying to query more than one taxonomy
array(
'taxonomy' => 'custom_tax_whatever',
'field' => 'term_id',
'terms' => array( 103, 115, 206 ),
),
),
'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',
),
)
);
PD:我没有测试过,但应该对你想要实现的目标有好处。
我正在使用 WooCommerce 来显示产品循环,并为同一循环构建了一个过滤器。过滤器使用 acf meta,其值直接从查询字符串中提取。 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' );
a var_dump of $tax_query 结果:
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"
}
}
}
我想在这里尽可能说清楚:
您要将 $meta_query_args 和 $tax_query_args 设置为不同的键:
您的 meta_query 将设置为:
$meta_query_args = 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',
),
);
$q->set( 'meta_query', $meta_query_args );
您的 tax_query 参数将设置为:(根据您的需要进行相应修改。)
$tax_query_args = array(
'relation' => 'AND', // AND , OR (Do not use with a single inner taxonomy array)
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'action', 'comedy' ),
),
//optionally if you are trying to query more than one taxonomy
array(
'taxonomy' => 'custom_tax_whatever',
'field' => 'term_id',
'terms' => array( 103, 115, 206 ),
),
);
$q->set( 'tax_query', $tax_query_args );
对于 meta_query 和 tax_query 键,您的最终查询应该大致如下所示:
array(
'tax_query' => array(
'relation' => 'AND', // AND , OR (Do not use with a single inner taxonomy array)
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'action', 'comedy' ),
),
//optionally if you are trying to query more than one taxonomy
array(
'taxonomy' => 'custom_tax_whatever',
'field' => 'term_id',
'terms' => array( 103, 115, 206 ),
),
),
'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',
),
)
);
PD:我没有测试过,但应该对你想要实现的目标有好处。