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.


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.

  • sanitize_title is used a lot. This could be tricky, where do you want this applied? In what context? Save? Display? What?
    – s_ha_dum
    Commented Mar 3, 2014 at 14:39
  • I want to use it on newposts of a particular custompost type at admin panel. The first code changes "Post*Title" to "post-title" OR "Post Title" to "posttitle" (however you adjust) when its first written on the title field. I want to only apply this effect to one custompost type. I want rest of the blog to function normally like "Post title" to "post-title". The first code works but the second one breaks and doesnt load anything on the title field. Commented Mar 3, 2014 at 14:46

1 Answer 1


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.


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).

  • Hey Kaiser I've tried your code (pasted in to my function.php file) but it doesnt effect neither the custom post or any other post. I've changed 'customposttype' with my customposttype slug. Perhaps i did it wrong? Also i didnt write this code but found it on the internet. It does what i want globally but i cannot narrow its response to particullar customposts. Commented Mar 3, 2014 at 15:13
  • Well, I thought you didn't just post a random snippet, but actually used sanitize_title() on your title somewhere. I'll file an edit, but so far it's mostly guessing and not really giving an answer to a real question. :/
    – kaiser
    Commented Mar 3, 2014 at 17:30
  • Hey Kaiser. Thanks alot for taking time to look in to this problem. As for the code; Oh, the original question was in here stackoverflow.com/questions/22135514/… Where i thought it was about Ajax. As ive mentioned before, it does what i want but it does it to all post types. Ive tried your code new code but it didnt do a thing again. Hope with me sharing the original question, it would make it a bit clearer. Can you may be check the original question and see if it makes sense? Thanks alot again Commented Mar 3, 2014 at 19:31
  • @user3067368 No, I'm not going to check that question. It would be much better if you edit your question and show us what the part of your template that calls the title looks like.
    – kaiser
    Commented Mar 3, 2014 at 22:24
  • Hey Kaiser, i've included further details in my original post. Hope this would show my intention a bit better. Commented Mar 3, 2014 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.