【发布时间】:2019-07-15 18:05:51
【问题描述】:
我正在尝试弄清楚如何从 Woocommerce 中最近查看的产品小部件中排除某个类别中的产品。
我知道可以使用以下代码从商店页面中删除/隐藏某个类别中的产品
function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'machine' ), // Don't display products in the machine category on the shop page.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
我想知道如何将“机器类别”中的产品排除在最近查看的产品小部件中。 (我正在使用搜索自动建议商店中可用的产品,它允许用户查看从存档页面/类别页面隐藏的产品),所以我想从最近查看的产品小部件中排除产品如果用户能够通过搜索访问产品。
我已使用此代码将某个类别中的产品排除在搜索结果中,这可以正常工作,但问题是自动建议仍然可以显示从查询中排除/隐藏的产品
function hello_pre_get_posts( $query ) {
if ( $query->is_search() ) {
$query->set( 'post_type', array( 'product' ) );
$tax_query = array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'machine',
'operator' => 'NOT IN',
),
);
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'hello_pre_get_posts' );
对于如何从“最近查看的产品”小部件中排除查看的产品的帮助将不胜感激。
【问题讨论】:
标签: php wordpress woocommerce product taxonomy-terms