You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用get_terms_args隐藏已隐藏父分类的子分类 - WooCommerce

Fixing WooCommerce Hidden Parent Categories & Their Child Terms Issue

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

  1. Incorrect Taxonomy Check: The condition 'product_cat' !== $taxonomies is flawed because $taxonomies is always an array when passed to this filter. This meant your code wasn't actually targeting product categories correctly in most cases.
  2. Forcing Top-Level Only: Setting $args['parent'] = 0 told 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.
  3. exclude_tree Conflict: When you tried exclude_tree, combining it with parent=0 likely 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 $taxonomies is 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_tree Usage: 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_cats are valid product category IDs (note: your original code had a duplicate 40—we removed that in the corrected version).
  • No other plugins or theme functions are modifying the get_terms_args filter with a higher priority (you can adjust the 10 in add_filter to a higher number like 20 if needed).

内容的提问来源于stack exchange,提问作者Naycom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 15:32:45