You can create your own plugin, or if intend on using the same theme throughout the entirety of the project, you can place this in your functions.php file. However I don’t recommend this because with theme updates this file is overwritten and keeping track of your custom code in that file alone — is not a best use case scenario. Especially if you don’t have a consistent back up solution in place.
The code below renames theĀ slug to the post type to its Post ID of each time the post is updated or created.
In this example there I use “CUSTOM-POST-TYPE” (you can change this to your own obviously). Also this only fires if the slug isn’t already the Post ID.
function rename_post_type_slug_to_id( $post_id ) {
$post = get_post( $post_id );
if ( $post->post_type === 'CUSTOM-POST-TYPE' ) {
// only apply this to the "CUSTOM-POST-TYPE'" post type
$new_slug = strval( $post_id ); // cast the post ID to a string
if ( $post->post_name !== $new_slug ) {
// only update the slug if it's different from the post ID
wp_update_post( array(
'ID' => $post_id,
'post_name' => $new_slug,
) );
}
}
}
add_action( 'save_post', 'rename_post_type_slug_to_id' );