UPDATE, the code is working now.
I have the following code but obliviously it doesn’t work as expected. It should take the terms from the taxonomy TAXONOMY_NAME only for custom post type CUSTOM_POST_TYPE and add as tags.
add_action('save_post','add_tags_auto');
function add_tags_auto($id) {
$terms = get_the_terms( $post->id, 'TAXONOMY_NAME' ); // get an array of all the terms as objects.
$add_tags = array();
foreach( $terms as $term ) {
$add_tags[] = $term->slug; // save the slugs in an array
}
$temp = array();
$tags = get_the_tags($id);
if ($tags) {
foreach ($tags as $tag)
$temp[] = $tag->name;
}
$tags = $temp;
$post = get_post($id);
if ($post->post_type != 'CUSTOM_POST_TYPE')
return false;
foreach ($add_tags as $t)
if (!in_array($t,$tags))
wp_add_post_tags($id,$add_tags);
}
This is untested, but it should be a step in the right direction, I’ve made several tweaks and will comment:
One potential problem I saw with your code was setting $post after trying to use it. I’m pretty sure when $post->id is called on the first line of your function PHP is raising a warning that $post is a non-object. So I’ve moved the call to get_post up to the top of the function.
A little tweak I added is to return early if the post is not the desired type. Better to do this ASAP, rather than spend time doing calculations that may just be discarded by the check further down the line.
I’ve also consolidated the number of foreach loops and renamed some of the variables for clarification. The only other thing I saw in your original code that may not have been working as expected was the call to wp_add_post_tags, it’s documented to take a comma delimited string as the second parameter, not an array.