Site icon Hip-Hop Website Design and Development

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

I need to auto update post title and slug with category name every time when post status is updated. Some function like this but with the correct code….

add_filter('wp_insert_post_data','reset_post_date',99,2);
   function reset_post_date($data,$postarr) {

   $data['post_title'] = How to add category name here?

 //also update the slug of the post for the URL
     $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'] ), $postarr['ID'], $data['post_status'], $data['post_type'], $data['post_parent'] );
     return $data;
  }

EDIT

I tried by myself but without success. Now I’m trying to run this function only when status is changed from draft to publish…but again no success.

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;
}

and

add_action('draft_to_publish', 'my_modify_title', 10, 2);

how to add filter and action in one function to make it work together?