wooccommerce产品类别搜索过滤器显示其他类别的产品



知道为什么这个查询显示不同父类别中的产品及其子类别吗?http://botanicaevents.com/rentals/?s=white&product_cat=花卉

它应该只显示产品"白色花朵产品测试"

White此查询正常工作:http://botanicaevents.com/rentals/?s=white&product_cat=租赁注意上面的工作字符串没有显示"白色花朵产品测试"

这是搜索表单:

<form role="search" method="get" id="searchform" action="http://botanicaevents.com/rentals/">
    <div>
        <label class="screen-reader-text" for="s">Search for:</label>
        <input type="text" value="" name="s" id="s" placeholder="Search for products" />
        <input type="hidden" name="product_cat" value="floral" />
        <input type="submit" id="searchsubmit" value="Search" />
    </div>
</form>

形式中唯一的变化是name="product_cat" value="rentals"

默认行为是WordPress将在提供的WooCommerce类别及其子类别中搜索搜索词。要更改它,您可能必须使用pre_get_posts操作来更改查询本身,使其不包括子类别。

例如:

add_action('pre_get_posts', 'woo_alter_category_search');
function woo_alter_category_search($query) {
    if (is_admin() || !is_search())
        return false;
    if ($product_cat = $query->get('product_cat')) {
        $query->set('product_cat', '');
        $query->set('tax_query', array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => $product_cat,
                'include_children' => false,
            )
        ));
    }
}

请记住,这段代码没有经过测试——它只是一个示例。

最新更新