I Need help please I have to hide Multiple product categories from my WordPress, WooCommerce, search and admin, This Coding is not working for me at all:
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' );
But the code doesn’t work. What I am doing wrong.
2
Answers
Udated
There are some few mistakes in your code. To hide products belonging to specific product categories from frontend search, use:
Now to hide product categories themselves, it’s something completely different, so you should ask a (some) new question(s), better explaining things and specifying which related plugins you are using.
Using get_terms_args you can exclude terms globaly.