WooCommerce:不同父分类下子分类Slug重复无法修改问题及解决方案
chairs-1 to chairs & How to Fix It Hey fellow WooCommerce dev, let's unpack this issue clearly:
The Root Cause
WordPress (which WooCommerce runs on) enforces global uniqueness for term slugs—even across different parent categories. When you created the second "Chairs" subcategory under "Used Furniture", WordPress detected that the slug chairs was already taken by the "New Furniture > Chairs" category. To avoid URL conflicts, it automatically appended -1 to the new category's slug. When you try to rename it back to chairs, the system blocks you because that slug is still registered to the other category—parent hierarchy doesn't override this default global uniqueness rule.
Fixes to Allow Duplicate Child Slugs
1. Add a Custom Code Snippet (Recommended for Full Control)
You can override WordPress's default slug check to only enforce uniqueness within the same parent category. Add this code to your theme's functions.php file, or use a code snippet plugin like Code Snippets to avoid modifying theme files directly:
add_filter( 'wp_unique_term_slug', 'allow_duplicate_product_cat_child_slugs', 10, 4 ); function allow_duplicate_product_cat_child_slugs( $slug, $term, $original_slug, $taxonomy ) { // Target only WooCommerce product categories if ( $taxonomy !== 'product_cat' ) { return $slug; } $parent_id = $term->parent; // Skip checks for top-level categories if ( empty( $parent_id ) ) { return $slug; } // Check if the slug is already used in the same parent category $existing_terms = get_terms( array( 'taxonomy' => $taxonomy, 'slug' => $original_slug, 'parent' => $parent_id, 'hide_empty' => false, 'exclude' => array( $term->term_id ) ) ); // If no duplicates in the same parent, use the original slug if ( empty( $existing_terms ) ) { return $original_slug; } // Fall back to system-generated slug if duplicates exist in the same parent return $slug; }
After adding the code:
- Go to WooCommerce > Products > Categories
- Edit the "Used Furniture > Chairs" category and set its slug to
chairs—it should save successfully now - Head to Settings > Permalinks and click "Save Changes" to flush WordPress's rewrite rules (this ensures the new URLs route correctly)
2. Use a Plugin (Quick No-Code Option)
If you don't want to work with code, use a plugin designed to allow duplicate slugs for terms. Plugins like Allow Duplicate Slugs or Custom Permalinks for WooCommerce can handle this, but be sure to test with your current WooCommerce version to avoid compatibility issues.
Important Note
Once fixed, both categories will have URLs /new-furniture/chairs and /used-furniture/chairs—WordPress will correctly route visitors to the right category based on the full URL path, so no frontend conflicts will occur!
内容的提问来源于stack exchange,提问作者Rowan van Zijl

