How to filter a custom post type by custom taxonomy without 404

When you want to use a taxonomy inside a custom post type URL hierarchy, you have to register it BEFORE the custom post type. Otherwise it won’t work properly. here is a short example:

function so305901_register_taxonomies() {
    $args = [
        //your settings
    ];
    register_taxonomy( 'taxonomy_slug', ['custom_post_type_slug'], $args );
}
add_action( 'init', 'so305901_register_taxonomies' );

function so305901_register_custom_post_types() {
    $args = [
        //your settings
        'taxonomies'    => [
            'taxonomy_slug'
        ],
    ];
    register_post_type( 'custom_post_type_slug', $args );
}
add_action( 'init', 'so305901_register_custom_post_types' );