Anonymous
О поиске в woocommerce Пользовательский таксономия не работает с «отношением» => 'и', но работает с «отношением» => 'или
Сообщение
Anonymous » 14 авг 2025, 22:31
Я пытаюсь создать поиск Woocommerce. Я использую WooCommerce 9.7 и последние WordPress. И поиск, который я хочу, - с несколькими таксономиями (категориями), поэтому я зарегистрировал еще 2 таксономия поверх продукта по умолчанию и продукту и продукту_бранда, вот вот код таксономии: < /p>
Код: Выделить всё
function custom_product_filter_taxonomies() {
register_taxonomy('vrsta', 'product', [
'label' => 'Vrsta',
'rewrite' => ['slug' => 'vrsta'],
'hierarchical' => true,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_in_rest' => true,
'query_var' => true,
'show_in_rest' => true,
]);
register_taxonomy('karakteristike', 'product', [
'label' => 'Karakteristike',
'rewrite' => ['slug' => 'karakteristike'],
'hierarchical' => true,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_in_rest' => true,
'query_var' => true,
'show_in_rest' => true,
]);
< /code>
}
add_action ('init', 'custom_product_filter_taxonomies'); < /p>
и моя форма поиска содержит: < /p>
input name="s"
input name="product_cat[]"
input name="product_brand[]"
input name="vrsta[]"
input name="kategorija[]"
input name="min_price"
input name="max_price"
< /code>
А вот запрос, который я пытаюсь изменить: < /p>
function woo_custom_filter_products_query($query) {
if (is_admin() || !$query->is_main_query() || !is_post_type_archive('product')) return;
$taxonomies = array('product_cat', 'product_brand', 'vrsta', 'karakteristike');
$filter_args = false;
foreach ($taxonomies as $taxonomy) {
if (isset($_GET[$taxonomy]) && !empty($_GET[$taxonomy])) {
//$terms = array_map('intval', array_filter((array) $_GET[$taxonomy]));
$terms = array_filter($_GET[$taxonomy]);
if (!empty($terms)) {
$filter_args[] = array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $terms,
'operator' => 'IN',
);
}
}
}
if(!empty($filter_args)) {
$taxquery = array_merge(array('relation' => 'OR'), $filter_args);
//$taxquery = array( 'relation' => 'OR', $filter_args );
$query->set('tax_query', $taxquery);
}
// Price filtering
if (isset($_GET['min_price']) || isset($_GET['max_price'])) {
$meta_query = $query->get('meta_query', array());
if (isset($_GET['min_price']) && is_numeric($_GET['min_price'])) {
$meta_query[] = array(
'key' => '_price',
'value' => floatval($_GET['min_price']),
'compare' => '>=',
'type' => 'NUMERIC'
);
}
if (isset($_GET['max_price']) && is_numeric($_GET['max_price'])) {
$meta_query[] = array(
'key' => '_price',
'value' => floatval($_GET['max_price']),
'compare' => '
Подробнее здесь: [url]https://stackoverflow.com/questions/79723605/on-woocommerce-search-custom-taxonomy-doesnt-work-with-relation-and-but[/url]
1755199883
Anonymous
Я пытаюсь создать поиск Woocommerce. Я использую WooCommerce 9.7 и последние WordPress. И поиск, который я хочу, - с несколькими таксономиями (категориями), поэтому я зарегистрировал еще 2 таксономия поверх продукта по умолчанию и продукту и продукту_бранда, вот вот код таксономии: < /p> [code]function custom_product_filter_taxonomies() { register_taxonomy('vrsta', 'product', [ 'label' => 'Vrsta', 'rewrite' => ['slug' => 'vrsta'], 'hierarchical' => true, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, 'show_in_rest' => true, 'query_var' => true, 'show_in_rest' => true, ]); register_taxonomy('karakteristike', 'product', [ 'label' => 'Karakteristike', 'rewrite' => ['slug' => 'karakteristike'], 'hierarchical' => true, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, 'show_in_rest' => true, 'query_var' => true, 'show_in_rest' => true, ]); < /code> } add_action ('init', 'custom_product_filter_taxonomies'); < /p> и моя форма поиска содержит: < /p> input name="s" input name="product_cat[]" input name="product_brand[]" input name="vrsta[]" input name="kategorija[]" input name="min_price" input name="max_price" < /code> А вот запрос, который я пытаюсь изменить: < /p> function woo_custom_filter_products_query($query) { if (is_admin() || !$query->is_main_query() || !is_post_type_archive('product')) return; $taxonomies = array('product_cat', 'product_brand', 'vrsta', 'karakteristike'); $filter_args = false; foreach ($taxonomies as $taxonomy) { if (isset($_GET[$taxonomy]) && !empty($_GET[$taxonomy])) { //$terms = array_map('intval', array_filter((array) $_GET[$taxonomy])); $terms = array_filter($_GET[$taxonomy]); if (!empty($terms)) { $filter_args[] = array( 'taxonomy' => $taxonomy, 'field' => 'term_id', 'terms' => $terms, 'operator' => 'IN', ); } } } if(!empty($filter_args)) { $taxquery = array_merge(array('relation' => 'OR'), $filter_args); //$taxquery = array( 'relation' => 'OR', $filter_args ); $query->set('tax_query', $taxquery); } // Price filtering if (isset($_GET['min_price']) || isset($_GET['max_price'])) { $meta_query = $query->get('meta_query', array()); if (isset($_GET['min_price']) && is_numeric($_GET['min_price'])) { $meta_query[] = array( 'key' => '_price', 'value' => floatval($_GET['min_price']), 'compare' => '>=', 'type' => 'NUMERIC' ); } if (isset($_GET['max_price']) && is_numeric($_GET['max_price'])) { $meta_query[] = array( 'key' => '_price', 'value' => floatval($_GET['max_price']), 'compare' => ' Подробнее здесь: [url]https://stackoverflow.com/questions/79723605/on-woocommerce-search-custom-taxonomy-doesnt-work-with-relation-and-but[/url]