Two functions, first one removes the slug from links throughout the site.
Second one adds the custom post type to the query, so that it looks for the name in both regular posts and custom post type.
function remove_cpt_slug( $link, $post ) { if ( 'my_post_type' === $post->post_type ) { $link = str_replace( '/' . $post->post_type . '/', '/', $link ); } return $link; } add_filter( 'post_type_link', 'remove_cpt_slug', 10, 2 ); function add_cpt_post_names_to_main_query( $q ) { if ( $q->is_main_query() && !empty( $q->query['name'] ) && !isset( $q->query['post_type'] ) ) { $q->set( 'post_type', [ 'post', 'my_post_type' ] ); } } add_action( 'pre_get_posts', 'add_cpt_post_names_to_main_query' );