Allow posts, pages and taxonomy to share the same permalink structure in WordPress.

Sometimes from an SEO perspective it is nice to have pages, taxonomy and posts that share the same permalink structure.

For example:

yoursite.com/updates (page)
yoursite.com/updates/category (taxonomy archive)
yoursite.com/updates/post-slug (post)

The issue with this is that when accessing the post, the /updates/[whatever] path matches WordPress’ pattern for taxonomy archives first, and will return a 404 error if the taxonomy term doesn’t exist.

This can be worked around as follows, where blog-category represents your taxonomy slug and your custom post type slug is blog.

function mytheme_template_redirect() {

    global $wp_query, $post;

    if (array_key_exists('blog-category', $wp_query->query)
        && $wp_query->is_404) {

        // WordPress thinks this is a blog category query e.g. /updates/[term slug]
        // but it could be a query for a blog post e.g. /updates/[blog post slug]

        $new_query = new WP_Query([
            'post_type' => 'blog',
            'posts_per_page' => 1,
            'name' => $wp_query->query['blog-category']
        ]);

        if (!empty($new_query->posts)) {

            status_header(200);
            nocache_headers();

            $wp_query = $new_query;
            $post = $new_query->posts[0];

        }

    }

}

add_action('template_redirect', 'yh2025_template_redirect');
Mobile Menu