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