It's a bit confusing, the documentation for wp_set_post_terms()
claims that the $taxonomy
argument is "Optional", but in looking at the source it appears that all this means is that its default value is post_tag
if you omit the argument. If you attempt to pass null
or an empty string, it won't work.
So I think the solution then would be to first get the taxonomies of the given IDs, and then set the terms for each taxonomy separately.
$post_id = 123; // For example.
$term_ids = [ 1, 2, 3 ]; // For example.
$terms = [];
foreach ( $term_ids as $term_id ) {
$term = get_term( $term_id );
$terms[$term->taxonomy][] = $term_id;
}
foreach ( $terms as $taxonomy => $term_ids ) {
wp_set_post_terms( $post_id, $term_ids, $taxonomy );
}