Child page parent and custom taxonomy share the same slug – 404 error on child page

14 Views Asked by At

I’ve got a custom taxonomy with the slug "topics" and a parent page that shares the same slug.

When adding child pages to the page with the slug "topics" all those children get a 404 error – the taxonomy term pages still work fine.

I stumbled upon this post that deals with the same issue but I think the crucial difference is that I need to get it to work with the built in post type "page" and not a custom registered post type: https://wordpress.stackexchange.com/questions/358157/rewrite-custom-post-taxonomy-to-share-same-url-path/358186#358186

function wpse_358157_parse_request3( $wp ) {
    $path      = 'topics';  // rewrite slug; no trailing slashes
    $taxonomy  = 'topic';   // taxonomy slug
    $post_type = 'page';    // post type slug

    if ( preg_match( '#^' . preg_quote( $path, '#' ) . '/#', $wp->request ) &&
        isset( $wp->query_vars[ $taxonomy ] ) ) {
        $slug = $wp->query_vars[ $taxonomy ];
        $slug = ltrim( substr( $slug, strrpos( $slug, '/' ) ), '/' );

        if ( ! term_exists( $slug, $taxonomy ) ) {
            $wp->query_vars['name']       = $wp->query_vars[ $taxonomy ];
            $wp->query_vars['post_type']  = $post_type;
            $wp->query_vars[ $post_type ] = $wp->query_vars[ $taxonomy ];
            unset( $wp->query_vars[ $taxonomy ] );
        }
    }
}
add_action( 'parse_request', 'wpse_358157_parse_request3' );
0

There are 0 best solutions below