使用get_terms_args隐藏已隐藏父分类的子分类 - WooCommerce
Let's work through your problem step by step—your current code has two key issues causing unwanted behavior and crashes, so we'll fix those and get your category exclusion working as intended.
First, Diagnose the Original Code Problems
- Incorrect Taxonomy Check: The condition
'product_cat' !== $taxonomiesis flawed because$taxonomiesis always an array when passed to this filter. This meant your code wasn't actually targeting product categories correctly in most cases. - Forcing Top-Level Only: Setting
$args['parent'] = 0told WooCommerce to only return top-level categories, which is why you were hiding all child categories entirely—this isn't what you want if you just need to exclude specific parent/child groups. exclude_treeConflict: When you triedexclude_tree, combining it withparent=0likely caused a logic conflict that led to the crash. Plus, your original check wasn't targeting the right taxonomy, so the filter might have been running in unintended contexts.
Corrected Function Code
Here's the revised code that properly excludes your specified parent categories and all their child categories, without hiding other valid categories or crashing:
add_filter( 'get_terms_args', 'mamaduka_edit_get_terms_args', 10, 2 ); /** * Exclude specific parent product categories and their entire child subtree from WooCommerce */ function mamaduka_edit_get_terms_args( $args, $taxonomies ) { // Skip modifications if we're in the admin area OR not dealing with product categories if ( is_admin() || !in_array( 'product_cat', $taxonomies ) ) { return $args; } // Array of parent category IDs you want to hide (this excludes their child categories too) $hidden_parent_cats = [50, 22, 20, 31, 35, 45, 40, 65, 37, 3434]; // Use exclude_tree to remove the parent categories AND all their child terms automatically $args['exclude_tree'] = $hidden_parent_cats; // Remove the parent parameter if it exists—we don't want to limit to only top-level categories if ( isset( $args['parent'] ) ) { unset( $args['parent'] ); } return $args; }
Key Changes Explained
- Fixed Taxonomy Check: We use
in_array('product_cat', $taxonomies)to correctly target product categories, since$taxonomiesis an array. - Removed
parent=0: This was forcing only top-level categories to show—removing it lets all valid categories (except your excluded ones) display, including child categories of non-hidden parents. - Proper
exclude_treeUsage: This parameter is designed exactly for your use case—it takes an array of parent term IDs and excludes those terms and all their descendants. No need to manually list every child category ID. - Admin Area Skip: We keep the admin area untouched so you can still manage all categories in the backend, which is usually the desired behavior.
If You Still Run Into Issues
If you experience crashes after this, double-check that:
- All IDs in
$hidden_parent_catsare valid product category IDs (note: your original code had a duplicate40—we removed that in the corrected version). - No other plugins or theme functions are modifying the
get_terms_argsfilter with a higher priority (you can adjust the10inadd_filterto a higher number like20if needed).
内容的提问来源于stack exchange,提问作者Naycom
相关产品推荐
相关产品推荐

