Without you sharing a post title and its resulting slug to see what is happening, I can only guess. But a lot of the time stop words, or extremely common words, are stripped from slugs. This would be words like the
, a
, an
, etc. The plugin Yoast SEO will do this if you’re running it as well.
You can override WordPress’ behavior though with the save_post
hook though.
function my_custom_slug( $post_id ) {
if ( ! wp_post_revision( $post_id ) ) {
// Temporarily Unhook to Prevent Infinite Loop
remove_action( 'save_post', 'my_custom_slug' );
// Set the new Post Slug
$my_slug = sanitize_title( get_the_post_title( $post_id ) );
// Update the Slug
wp_update_post( array(
'ID' => $post_id,
'post_name' => $my_slug,
) );
// Rehook
add_action( 'save_post', 'my_custom_slug' );
}
}
add_action( 'save_post', 'my_custom_slug' );
So what this code will do is, when the post is saved, take the post_title (e.g. This is the title of my post
), sanitize it by removing HTML/PHP code as well as special characters, making it all lower-case letters, and converting all white space to -
, storing it as $my_slug
. So using my example, the title would become this-is-the-title-of-my-post
. Then it will update the post_name
, which contains the slug with the value of $my_slug
.
NOTE: If you’re doing this with a custom post type, the procedure is a little bit different and will need some changes and additional code in order to work.