【发布时间】:2022-02-06 05:07:43
【问题描述】:
我正在使用 woocommerce 产品简码在产品页面上显示一些相关产品。
产品简码如下:
do_shortcode('[products limit="6" columns="6"]');
我正在使用woocommerce_shortcode_products_query 过滤器修改简码,如下所示:
add_filter( 'woocommerce_shortcode_products_query', function( $query_args, $atts, $loop ){
$product_id = 12345;
// Remove out of stock results
$query_args['meta_query'] = array( array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT LIKE',
) );
// Remove the current product by id
$query_args['tax_query'] = array( array(
'taxonomy' => 'product',
'field' => 'id',
'terms' => array($product_id), // Remove this product from the shortcode results
'operator' => 'NOT IN',
) );
return $query_args;
}, 10, 3);
第一部分通过删除缺货结果来工作。
第二部分不起作用,我无法弄清楚如何从查询中排除特定产品。
谷歌搜索问题时的所有结果都在谈论 product_cat 分类,但是我不想删除某个类别中的产品,我想删除当前产品by id,以便仅在短代码中显示其他相关产品。
所以我的问题是:如何使用“woocommerce_shortcode_products_query”从产品简码中隐藏当前产品
【问题讨论】:
标签: wordpress woocommerce wordpress-theming wordpress-shortcode woocommerce-theming