从WooCommerce相关部分中删除具有特定标签的产品

时间:2020-09-26 10:14:46

标签: wordpress woocommerce

在WooCommerce官方文档中,我找到了一个代码,该代码从主页上排除了具有特定标签(例如“仅针对女士”)的产品。效果很好,但我也想在单个产品页面的相关产品部分中排除这些产品。

我试图将此行添加到“ if”部分,但似乎不起作用:

if ( ! $q->is_product() && $woocommerce_loop['name'] == 'related' ) return;

原始代码

add_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;

    $q->set( 'tax_query', array(array(
        'taxonomy' => 'product_tag',
        'field' => 'slug',
        'terms' => array( 'only-for-women' ), 
        'operator' => 'NOT IN'
    )));

    remove_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

0 个答案:

没有答案