Custom Postype specific changes in admin panel

I have this small bit of code which does exactly what I need. But I want to limit its use to one Custom Post Type (CPT) only.

add_filter('sanitize_title', 'my_custome_slug');
function my_custome_slug($title) {
    return str_replace('-', '', $title);
}

I’ve tried the following code with no luck:

function my_custome_slug($title){ 
global $post;
    if ( $post->post_type == 'customposttype') {
         return str_replace('*', '-', $title);
         }
    } 
add_filter('sanitize_title', 'my_custome_slug'); 

Any help is appreciated.

EDIT:

Sorry i was not clear in my post.

WordPress is taking “Post Titles” and change them to “post-titles” in urls when we first enter the title in any new posts. My original question was , for a specific custom post type, i need to remove “-” in urls of the posts. So they will be “posttitles”

enter image description here

I thought the problem was using “sanitize_title” because every other example i found for custom post specific content loading in Admin panel works. But once i use sanitize_title in those examples, the result would simply make a blank url under the title field.

The first code i’ve shared is doing this already. I’ve tried to limit it’s function to a specific custom posttype but it simply didnt work.

I need this feature and i saw its already possible. I just need to limit it to a specific custom post type. This is rather a backend problem (due to some internal structre) and not a frontend problem. Otherwise i would have tried it with htacess. So our main target is wordpress publish page on admin panel.

Solutions Collecting From Web of "Custom Postype specific changes in admin panel"

A filter always needs to return something. So in the second example you should try return $title; after the if statement so it doesn’t break other posts.

add_filter( 'sanitize_title', 'my_custome_slug' );
function my_custome_slug( $title )
{ 
    return ( 'customposttype' === $GLOBALS['post']->post_type )
        ? str_replace( '*', '-', $title )
        : $title;
}

I’m not completely sure why you’re hooking that callback to sanitize_title. Imo it would be easier to just save the post different and using the save_post hook to alter the title.

Edit

As previously guessed and as the OP stated in the comments (that above was just a random snippet found on the internet), here’s an update how to actually alter a posts title.

The function get_the_title() gets called by the function the_title() and either one or the other should be used to output the title for posts, pages and custom post types. get_the_title() returns the title inside a filter:

return apply_filters( 'the_title', $title, $id );

So the easiest way is to add a callback on exactly that and alter the output there:

add_filter( 'the_title', 'wpse136615_title_str_replace', 10, 2 );
function wpse136615_title_str_replace( $title, $id )
{
    $post = get_post( $id );
    return ( 'your_custom_post_type_name' === $post->post_type )
        ? str_replace( '*', '-', $title )
        : $title;
}

Simply replace your_custom_post_type_name with your real custom post types name (see the arguments used in register_post_type() if you’re unsure about the name).