You can use wp_set_object_terms()
to assign taxonomy terms to a post.
wp_set_object_terms($post_id, $terms, $taxonomy, true);
Where $terms
– A single term slug, single term id, or array of either term slugs
or ids. Will replace all existing related terms in this taxonomy.
See documentation: https://codex.wordpress.org/Function_Reference/wp_set_object_terms
Edit:
In your case, it will look like:
wp_set_object_terms($post_id, array(2,3,4,5), 'category', true);
Please check for any syntax error.