Add a new tag based on the category name in the publish event

With this below, an action that overwritten all existing tags by catname, how to add a new tag, preserving the current tags, and not only in update, but in publish event, since I use an import plugin, and currently I have to enter all the posts and click on update to take the desired action.

add_filter('wp_insert_post', 'add_cat_to_tags', 10, 3 );
function add_cat_to_tags( $post_ID, $post, $update ) {
   $tags = array();
   $cats = get_the_category( $post_ID );
   foreach ( $cats as $cat ) {
      $tags[] .= $cat->name;
   }
   // overwrites any existing tags!!
   wp_set_post_tags( $post_ID, $tags );
}

Thanks all.

Read more here: Add a new tag based on the category name in the publish event

Leave a Reply

Your email address will not be published. Required fields are marked *