提问者:小点点

显示其他类别产品的woocommerce产品类别搜索筛选器


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

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

白色此查询正常工作:http://botanicaevents.com/rentals/?s=white

这是搜索表:

<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"


共1个答案

匿名用户

默认行为是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,
            )
        ));
    }
}

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