WordPress custom post type URL rewrite causing 404 errors and slug conflicts

I’m working with a WordPress site where I set up a custom post type called trip along with a custom taxonomy destination-category. I’m running into two main issues.

First, my single post pages are showing 404 errors even though the taxonomy archives work fine. Second, I’m having problems with duplicate slugs getting modified automatically.

<?php

add_action('init', 'register_custom_taxonomy');
function register_custom_taxonomy()
{
    register_taxonomy('destination-category', ['trip'], array(
        'labels' => array(
            'name'              => 'location',
            'all_items'         => 'all locations',
            'edit_item'         => 'edit location',
            'update_item'       => 'update location',
            'add_new_item'      => 'add new location',
        ),
        'public' => true,
        'hierarchical' => true,
        'rewrite' => array(
            'slug' => 'trip',
            'with_front' => false,
            'hierarchical' => true,
        ),
        'show_admin_column' => true,
        'show_in_rest' => false,
    ));
}

add_filter('post_type_link', 'trip_permalink_structure', 10, 2);
function trip_permalink_structure($link, $post_obj) {
    if ($post_obj->post_type !== 'trip') return $link;

    $categories = get_the_terms($post_obj->ID, 'destination-category');
    if (!empty($categories) && !is_wp_error($categories)) {
        return str_replace('%destination-category%', $categories[0]->slug, $link);
    } else {
        return str_replace('%destination-category%', 'general', $link);
    }
}

add_action('init', 'register_trip_post_type');
function register_trip_post_type()
{
    register_post_type('trip', array(
        'label' => 'Travel Package',
        'public' => true,
        'rewrite' => array(
            'slug' => 'trip/%destination-category%',
            'with_front' => false,
            'hierarchical' => true
        ),
        'has_archive' => 'trip',
        'supports' => array('title', 'editor', 'thumbnail'),
        'taxonomies' => array('destination-category'),
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_admin_bar' => true,
        'hierarchical' => true,
        'menu_icon' => 'dashicons-location-alt',
        'labels' => array(
            'name' => 'trip',
            'singular_name' => 'trip',
            'menu_name' => 'trips',
            'add_new_item' => 'add trip',
            'edit_item' => 'edit trip',
            'new_item' => 'new trip',
            'view_item' => 'view',
            'search_items' => 'search trips',
            'not_found' => 'nothing found',
            'not_found_in_trash' => 'trash is empty',
        )
    ));
}

My URLs look like this:
Taxonomy page: site.com/trip/europe/france/paris/
Single post: site.com/trip/europe/france/paris/paris-weekend-package-3days

The taxonomy pages work perfectly, but single posts return 404 errors.

Also, when I create terms with the same slug under different parents (like weekend-special under both paris and london), WordPress automatically changes the second one to weekend-special-london. Same thing happens with post slugs.

How can I fix the 404 issue and handle the duplicate slug problem? Any help would be great.

everyone’s talking about rewrite rules, but you’re missing the parse_request hook. wordpress won’t handle your custom structure without it. also, test if your post_type_link filter is actually working - throw a wp_die() in there temporarily to check.

I’ve dealt with this exact custom post type mess for years - you’re actually facing two different problems here. First, those 404 errors happen because your rewrite rule assumes exactly 4 URL segments, but hierarchical taxonomies can have different depths. Stop hardcoding the pattern. Use this instead: add_rewrite_rule('^trip/(.+?)/([^/]+)/?$', 'index.php?post_type=trip&name=$matches[2]', 'top'); The .+? does non-greedy matching and handles any taxonomy depth before your post slug. For duplicate slugs - that’s just how WordPress works, and honestly? You might want to roll with it instead of fighting core behavior. Those automatic suffixes actually help with SEO since search engines like unique URLs. But if you really need identical slugs across different taxonomy branches, hook into the wp_unique_post_slug filter and modify the uniqueness check to work within taxonomy terms instead of globally. Don’t forget to flush permalinks after making rewrite changes.

Yeah, rewrite rules work but they get messy fast with complex hierarchies. You’ll end up writing tons of regex patterns for different depth levels.

I’ve hit this exact problem before. Automating the URL handling beats manual WordPress tweaking every time. Instead of fighting WordPress rewrite rules, I set up automated workflows that handle URL structure and content routing.

The automation generates permalinks, catches 404s, and redirects them based on your taxonomy hierarchy. It also fixes duplicate slugs by keeping a centralized registry that prevents conflicts automatically.

For duplicates, the system can append parent category info or generate unique identifiers without breaking your URLs. Way cleaner than WordPress default behavior.

I use this on several sites with complex custom post types and it kills all the rewrite rule headaches and permalink conflicts. The automation runs in the background and keeps everything smooth.

You can set this up easily with Latenode - it handles all the URL management and routing automatically: https://latenode.com

you’re missing rewrite rules for single posts. add this to your init function:

add_rewrite_rule('^trip/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$', 'index.php?post_type=trip&name=$matches[4]', 'top');

don’t forget to flush permalinks afterward - just go to settings > permalinks and hit save.

as for the duplicate slug issue, that’s just how wordpress works by default. you’d need custom code to change it.

This URL structure is way too complex for WordPress rewrite rules. Every time you add a taxonomy level or change hierarchy, you’re writing new regex patterns.

I’ve watched this kill sites at scale. You need external automation that handles URL routing completely outside WordPress.

Set up automation that intercepts requests before they hit WordPress, parses your custom URLs, and routes to the right content. It handles any taxonomy depth without hardcoded patterns.

For duplicate slugs, the automation uses smart naming that prevents conflicts while keeping URLs clean. Instead of ugly suffixes like “weekend-special-london”, it uses context-aware routing where the same slug works under different parents.

It also prevents 404s by auto-creating redirects when URLs change and maintaining a master registry of all your custom post URLs.

This completely bypasses WordPress permalink limitations and gives you total control. Plus it scales without breaking when you add more taxonomy levels or post types.

I’ve built this exact solution for complex travel sites with multiple custom post types and deep taxonomies. Works flawlessly.

Latenode handles all this URL automation perfectly: https://latenode.com

The 404 error occurs because WordPress may not recognize your custom post type’s URL structure. To address this, ensure you have the correct rewrite rules that match your desired permalink pattern. After registering your post type, add the following: php add_action('init', 'add_trip_rewrite_rules'); function add_trip_rewrite_rules() { add_rewrite_rule('^trip/(.+)/([^/]+)/?$', 'index.php?post_type=trip&name=$matches[2]', 'top'); } Regarding the duplicate slug issue, keep in mind that WordPress requires unique slugs globally. You can work around this by utilizing the wp_unique_post_slug filter, allowing duplicates within the same taxonomy. Just ensure to flush your permalinks after updating the rules, as they are essential for the proper hierarchy.