How to auto update post title and slug with category name when post status is updated

You need post status transitions, and quite similar with this post:

only what you need to do update post title and slug, and this is easy.

Update post examples


Since you wrote like this:

add_filter( 'the_title', 'my_modify_title', 10, 2 );
 function my_modify_title( $title, $id ) {
  if( in_category( 'My Category', $id ) ) {
   $title="My Category";
 }
  if( in_category( 'New Category', $id ) ) {
   $title="New Category";
 }
return $title;
}

Note that codex is saying ‘the_title’ should work only inside the loop. I would never update slugs inside loop. The loop looks to me more like a way to draw the post content.

Also note, if trying to update the title:

$title = $title. "My Category";

would be more common.

What I would suggest:

add_action('draft_to_publish', 'my_modify_title', 10, 2);
function my_modify_title ($post){
   // your work to update post slug and title.

    $current_title = $post->post_title;
    $current_slug = $post->post_name;

    $new_slug = ...;
    $new_title = ...;



    wp_update_post(
        array (
            'ID'        => $post->ID,
            'post_name' => $new_slug, 
            'post_title'=> $new_title
        )
    );

   return;    
}

And you have the last code inside your functions.php or some even better place.

Check out this ref: