WordPress: Add Parent Term Slug to Child Term URL

By default, hierarchical taxonomy terms do not include the parent slug in their URL. A child term “Lorem” under a parent “Test” has the URL /lorem/, not /test/lorem/. Adding the parent slug creates cleaner, more descriptive URLs that reflect the taxonomy hierarchy.

Problem: When registering a hierarchical taxonomy, child term URLs do not include the parent term slug by default — so /category/child/ is generated instead of /category/parent/child/.

Solution: Pass 'hierarchical' => true inside the rewrite argument array when calling register_taxonomy() — WordPress will then include parent slugs in child term URLs automatically, matching the behaviour of the built-in category taxonomy.

The fix is a single argument when registering the taxonomy. Add 'hierarchical' => true inside the rewrite array:

register_taxonomy( 'my-taxonomy', 'post', [
    'label'        => __( 'My Taxonomy', 'textdomain' ),
    'hierarchical' => true,
    'rewrite'      => [
        'slug'         => 'topics',
        'hierarchical' => true,   // ← this is the key argument
        'with_front'   => false,
    ],
] );

With this setting, a term structure like:

Topics
└── Design
    ├── Typography
    └── Colour Theory

produces URLs like:

/topics/design/
/topics/design/typography/
/topics/design/colour-theory/

After changing rewrite arguments, always flush the rewrite rules so WordPress rebuilds the URL patterns:

wp rewrite flush

Or navigate to Settings → Permalinks and click Save Changes.

NOTE: This rewrite setting only affects new URL generation. Any existing links to child term pages (e.g. in internal menus or stored in the database) will break after the change until they are updated to use the new URLs with the parent slug prefix. Use WP-CLI's wp search-replace to update stored URLs.