WooCommerce-从价格范围查询中隐藏产品类别



我目前正在使用默认的WooCommerce Widget功能,以url格式按价格范围查询产品:www.mydomain.com/shop/?min_price=50&max_price=100

我现在正试图从这个查询中排除一个特定的类别——让slug为hidden-category

我尝试了以下代码,但问题是,当我转到时,这也会删除所有结果

www.mydomain.com/category/hidden-category/

function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'hidden-category' ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' ); 

这就是我最终要做的:

第一个函数从搜索结果中隐藏hidden-category中的所有内容,第二个函数从价格范围查询中隐藏它。

//hide sold from search results
function sm_pre_get_posts( $query ) {
if (  $query->is_search() ) {
$query->set( 'post_type', array( 'product' ) );
$tax_query = array(
array(
'taxonomy' => 'product_cat',
'field'   => 'slug',
'terms'   => 'hidden-category', //slug name of category
'operator' => 'NOT IN',
),
);
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'sm_pre_get_posts' );
//hide from query
function custom_pre_get_posts_query( $q ) {
if (!$q->is_main_query() || !is_shop() ) return;
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'hidden-category' ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

最新更新