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

时间:2014-10-15 18:46:28

标签: wordpress woocommerce

知道为什么这个查询在不同的父类别中显示产品并且它是孩子的​​? http://botanicaevents.com/rentals/?s=white&product_cat=floral

它只应显示产品“白色花卉产品测试”

白色此查询正常运行:http://botanicaevents.com/rentals/?s=white&product_cat=rentals 注意上面的工作字符串不显示“白色花卉产品测试”

以下是搜索表单:

<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 个答案:

答案 0 :(得分:0)

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

请记住,此代码未经过测试 - 这只是一个示例。

相关问题