Site icon Hip-Hop Website Design and Development

Cheap WordPress custom taxonomy URL rewrite on spelling errors

I have found myself in a situation which I would like to understand and ultimately fix.
We have custom post types and for them, custom taxonomies.

Everything is working fine. URLs a generated right. But: when, for whatever reason, someone is changing a part of the URL or it’s misspelled, there is something strange happening (in my opinion). Calling this correct URL first:

/treatment/psychosomatic/psychosomatic-dysfunction/temporary-psychomatic-dysfunction/
/<custom post type>/<taxonomy level 0>/<taxonomy level 1>/<taxonomy level 2>/

and changing the URL by entering something completely different in an URL part, the content of the original URL is shown and also indexed and returning HTTP status code 200, which may be a SEO problem in the future. It also doesn’t matter which URL part is changed, except the last one. So this still shows the content of the original page.

/treatment/exampletext/lorem-ipsum-dolor/temporary-psychomatic-dysfunction/

The question is, how to change the behaviour so that misspelled URLs will cause a 404 error?

Here are the post type and taxonomy registration codes:

function cptui_register_my_cpts_clinic() {

$labels = [
    "name" => __( "clinics", "spotter" ),
    "singular_name" => __( "clinic", "spotter" ),
];

$args = [
    "label" => __( "clinics", "spotter" ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => true,
    "rest_base" => "",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    "has_archive" => false,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "delete_with_user" => false,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => [ "slug" => "clinic", "with_front" => false ],
    "query_var" => true,
    "menu_icon" => "dashicons-admin-home",
    "supports" => [ "title", "editor", "thumbnail", "comments" ],
    "show_in_graphql" => false,
];

register_post_type( "clinic", $args );}

and

function cptui_register_my_taxes_treatment() {

$labels = [
    "name" => __( "treatments", "spotter" ),
    "singular_name" => __( "treatment", "spotter" ),
];


$args = [
    "label" => __( "Treatment", "spotter" ),
    "labels" => $labels,
    "public" => true,
    "publicly_queryable" => true,
    "hierarchical" => true,
    "show_ui" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "query_var" => true,
    "rewrite" => [ 'slug' => 'treatment', 'with_front' => false,  'hierarchical' => true, ],
    "show_admin_column" => false,
    "show_in_rest" => true,
    "rest_base" => "treatment",
    "rest_controller_class" => "WP_REST_Terms_Controller",
    "show_in_quick_edit" => true,
    "show_in_graphql" => false,
];
register_taxonomy( "treatment", [ "clinic" ], $args );}

I hope the information is enough to see the problem. I’m happy to show more relevant code if needed.