how to add tags to post in the Loop?

I found the issue was with the polylang plugin. For the above to work I needed to add the language info and in particular associated the tag with the language of the post.

$result=wp_insert_term($tag, ‘post_tag’); pll_set_term_language($result[“term_id”], ‘fr’);

pll_set_term_language is the function provided by the polylang plugin for associating the newly added tag with the language (fr=French in the above example).