You will need to leverage the post_updated
hook API of WordPress
// You will need to leverage the <code>save post</code> hook API of WordPress
add_action( 'post_updated', 'save_post_callback', 10, 3 );
//add_action('save_post', 'save_post_callback');
function save_post_callback($post_ID, $post_after, $post_before){
global $post;
if ($post->post_type != 'page'){
return;
}
// If you get here then it's your post type so do your thing....
// unhook this function so it doesn't loop infinitely
remove_action('post_updated', 'save_post_callback');
$title = $post_after->post_title;
// Check if "NTS00" is already there,
// Deliberately checking if position found is > 0
if( strpos($title, "NTS00") == FALSE ){
// Set the title
$args = array(
'ID' => $post_ID,
'post_title' => $title.'_NTS000'.$post_ID
);
// update the post, which calls save_post again
wp_update_post( $args );
// Bonus!
update_post_meta( $post_ID, 'my_key', 'NTS000'.$post_ID );
// re-hook this function
add_action('post_updated', 'save_post_callback');
}
}
This way you will ensure that the NTS00 gets appended only once.
Bonus feature will ensure that you also store that value for a “custom key” as post meta.
I hope I was able to help.