Once a post is published in WordPress, its slug does not change on its own. Even if you edit the title later, the URL stays the same.
That is intentional. Updating a public URL without setting up a redirect breaks existing links, affects search rankings, and sends visitors to a dead page.
There are cases where automatic slug updates make sense, especially in custom workflows. This guide walks through the process step by step. It uses WordPress hooks to update post's permalink based on specific triggers, without installing any additional plugins.
How to Set Post Permalink Programmatically?
Even though WordPress does not have any out-of-box functionality that would allow o automatically update permalinks, you can make it happen with an extra code snippet.
When updating permalinks programmatically in WordPress, the most convenient way is to use the wp_update_post() function.
wp_update_post( array( 'ID' => $post_id, 'post_name' => $new_slug ) );
The snippets below show simple examples of how you can trigger a permalink update when specific conditions are met during a post update.
SEO Considerations
In terms of your SEO rank, after the post or page is published, it is generally recommended to keep the permalinks consistent. Since search engines may have already indexed your URLs, making changes to them may affect backlinks and result in broken links and lost SEO ranking.
However, there are scenarios in which it still may be advantageous to automatically update the URLs. For instance, if the title of your post contains a year (for instance, "Best Smartphones of 2024"), you might want to have this automatically updated every year in order to make sure that the content remains relevant.
Make sure you do not update your already-indexed permalinks unless you really have to. If you do need to alter them, make sure to properly redirect the old URLs to the new ones in order to maintain your search engine rankings and user experience.
Code Examples
When adding code snippets to WordPress, avoid editing your parent theme's functions.php directly. That file gets overwritten every time the theme updates, erasing anything you added manually.
The most common approach is to use a child theme, which stores your customizations separately from the parent theme.
If you do not have a child theme yet, you can follow this guide to create one and generate a ready-to-install ZIP file in a few steps.
Changed Post Title
function pm_update_slug_on_title_change( $post_id, $post_after, $post_before ) {
	$post = get_post( $post_id );
	if ( $post_after->post_type == 'post' && $post_after->post_title !== $post_before->post_title ) {
		$new_slug = sanitize_title( $post_after->post_title );
		wp_update_post( array(
			'ID' => $post_id,
			'post_name' => $new_slug
		) );
	}
}
add_action( 'post_updated', 'pm_update_slug_on_title_change', 9, 3 );
Changed Assigned Category
function pm_update_slug_on_term_change( $post_id, $terms, $tt_ids, $taxonomy ) {
	if ( $taxonomy == 'category' ) {
		$new_slug = sanitize_title( $post_after->post_title );
		wp_update_post( array(
			'ID' => $post_id,
			'post_name' => $new_slug
		) );
	}
}
}
add_action( 'set_object_terms', 'pm_update_slug_on_term_change', 9, 4 );
Changed Custom Field Value
function pm_update_slug_on_custom_field_change( $meta_id, $post_id, $meta_key, $meta_value ) {
	if ( $meta_key == 'sample-custom-field' ) {
		$new_slug = sanitize_title( $post_after->post_title );
		wp_update_post( array(
			'ID' => $post_id,
			'post_name' => $new_slug
		) );
	}
}
}
add_action( 'updated_postmeta', 'pm_update_slug_on_custom_field_change', 9, 4 );
Leave a Reply