0

As you can see in this action, it is overwriting 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 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.

0

You have to add the true parameter to the wp_set_post_tags() function. Tested and works, the corrected code:

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, true );
}
  • Thanks @ciaik for reply, yes, this solution is to preserve existing tags, but before, I already had one that worked even without me understanding why! Adding within forech $post_content [] = $cat-> name; And too add parameter $post_ID, $tags, $post_content !! I confess that I do not understand, but it always worked! What I really need, what I really need is the other request, but it only works on manual posts, I need this to happen when the plugin auto posts, auto import, and I having to go into the post and click publish/update for the tags to be add! – hercules Apr 19 '17 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.