从 WooCommerce 搜索中隐藏产品类别

问题描述 投票:0回答:2

我需要帮助,我必须从我的 WordPress、WooCommerce、搜索和管理中隐藏多个产品类别,此编码根本不适合我:

function exclude_product_categories( $q ) {
    $excluded_cats = array(
        3233, 3254, 3244, 1990, 3120, 3129, 3121, 2717, 2718, 2355,
        3314, 2356, 3040, 3312, 2978, 2357, 3125, 3117, 2358, 1994,
        3333, 1995, 2998, 2699, 3123, 2714, 2700, 1991, 3330, 3324,
        1992, 3325, 2862, 3122, 3328, 2013, 3156, 2364, 2479, 3246,
        2701, 3256, 3166, 2698, 2353, 3115, 3116, 3251, 3329, 3331,
        3250, 2010, 2011, 2171, 2715, 2796, 2980, 2981, 3124, 3255,
        3243, 2894, 1984, 1985, 1986, 3231, 3101, 3105, 3102, 3109,
        3103, 2418, 2419, 3253, 2814, 3170, 1987, 1988, 2015, 2996,
        2017, 3012, 2989, 2570, 3234, 2571, 2006, 2007, 2004, 3230,
        2012, 2695, 2864, 3235, 3005, 2466, 2908, 2909, 2910, 2008,
        2682, 2786, 2991, 2812, 2019, 2020, 2900, 2791, 3241, 3238,
        3357, 3358, 2904, 2905, 3245, 2893, 2459, 2795, 2906, 2897,
        2907, 2684, 3153, 3104, 2902, 3240, 2349, 2350, 2351, 3377,
        3161, 3162, 3247, 3248, 3376, 3249, 3239, 1996, 2001, 2792,
        2912, 2002, 2990, 2003, 2567, 2568, 2374, 2375, 2456, 3257,
        3154, 1999, 2354, 2000, 2895, 3164, 2896, 2021, 3293, 2022,
        3155, 3147, 3167, 3313, 2697, 2911, 2457, 3237, 2901, 2683,
        2898, 2903, 2719, 2793, 2794, 2876, 2877, 2720, 2995, 3252,
        1193, 1205, 1201, 1203, 1511, 3480, 1697, 3165, 1250, 1734,
        1899, 1874, 1729, 1853, 1204, 3363, 1176
    );

    if( ( $q->is_post_type_archive('product') || $q->is_tax('product_cat') ) && $q->is_main_query() ) {
        $tax_query = (array) $q->get('tax_query');
        $tax_query[] = array(
            'taxonomy' => 'product_cat',
            'field' => 'term_id',
            'terms' => $excluded_cats,
            'operator' => 'NOT IN'
        );
        $q->set('tax_query', $tax_query);
    }
}
add_action( 'pre_get_posts', 'exclude_product_categories' );

但是代码不起作用。我做错了什么。

php wordpress woocommerce product custom-taxonomy
2个回答
1
投票

已更新

您的代码中有一些错误。要在前端搜索中隐藏属于特定产品类别的产品,请使用:

// Settings: Here your product categories array
function hidden_product_categories() {
    return array(
        3233, 3254, 3244, 1990, 3120, 3129, 3121, 2717, 2718, 2355,
        3314, 2356, 3040, 3312, 2978, 2357, 3125, 3117, 2358, 1994,
        3333, 1995, 2998, 2699, 3123, 2714, 2700, 1991, 3330, 3324,
        1992, 3325, 2862, 3122, 3328, 2013, 3156, 2364, 2479, 3246,
        2701, 3256, 3166, 2698, 2353, 3115, 3116, 3251, 3329, 3331,
        3250, 2010, 2011, 2171, 2715, 2796, 2980, 2981, 3124, 3255,
        3243, 2894, 1984, 1985, 1986, 3231, 3101, 3105, 3102, 3109,
        3103, 2418, 2419, 3253, 2814, 3170, 1987, 1988, 2015, 2996,
        2017, 3012, 2989, 2570, 3234, 2571, 2006, 2007, 2004, 3230,
        2012, 2695, 2864, 3235, 3005, 2466, 2908, 2909, 2910, 2008,
        2682, 2786, 2991, 2812, 2019, 2020, 2900, 2791, 3241, 3238,
        3357, 3358, 2904, 2905, 3245, 2893, 2459, 2795, 2906, 2897,
        2907, 2684, 3153, 3104, 2902, 3240, 2349, 2350, 2351, 3377,
        3161, 3162, 3247, 3248, 3376, 3249, 3239, 1996, 2001, 2792,
        2912, 2002, 2990, 2003, 2567, 2568, 2374, 2375, 2456, 3257,
        3154, 1999, 2354, 2000, 2895, 3164, 2896, 2021, 3293, 2022,
        3155, 3147, 3167, 3313, 2697, 2911, 2457, 3237, 2901, 2683,
        2898, 2903, 2719, 2793, 2794, 2876, 2877, 2720, 2995, 3252,
        1193, 1205, 1201, 1203, 1511, 3480, 1697, 3165, 1250, 1734,
        1899, 1874, 1729, 1853, 1204, 3363, 1176
    );
}

add_action( 'pre_get_posts', 'exclude_product_categories' );
function exclude_product_categories( $q ) {
    if ( $q->is_search() && ! is_admin() ) {
        $tax_query = (array) $q->get('tax_query');

        $tax_query[] = array(
            'taxonomy' => 'product_cat',
            'field'    => 'term_id',
            'terms'    => hidden_product_categories(),
            'operator' => 'NOT IN'
        );

        $q->set('tax_query', $tax_query);
    }
}

现在隐藏产品类别本身,这是完全不同的事情,所以你应该问一个(一些)新问题,更好地解释事情并指定你正在使用哪些相关插件。


0
投票

使用 get_terms_args 您可以全局排除术语。

function exclude_terms_by_ids($args) {
    // Define an array of term IDs to exclude.
    $excluded_term_ids = array(1,2,3); // Replace with the IDs you want to exclude.

    $args['exclude'] = $excluded_term_ids;

    return $args;
}
add_filter('get_terms_args', 'exclude_terms_by_ids', 10, 2);
© www.soinside.com 2019 - 2024. All rights reserved.