I'm trying to add a number of tags when a post is saved or added. It should just append if they don't exist and if they already exist for that object just ignore them.
This is a short version after hours of looking; something like this is supposed to work I would think, but it doesn't:
function generate_tags($id) {
$tags = array("testtag1"); // just an example
wp_set_object_terms((int)$id, $tags, 'post_tag', true);
}
add_action('edit_post', 'generate_tags', 99);
It does add the testtag1 to wp_terms and to wp_term_taxonomy.
Now the problem is that it calls wp_set_oject_terms after save of the object a lot of times, and most times with false as last parameter and no tags at all, so it removes the tags it just added and then stops.
I have no idea how to prevent this behaviour or how to fix this and I have tried a lot. What am I doing wrong?