Set post terms by term id

When dealing with this, it turned out that the $parent var had to be casted as an int. Looking at the function wp_set_object_terms, it tests the param $parent with is_int(). The var $parent was coming from $_POST which was sent though http post using ajax.

Somewhere along the way, probably on the javascript side, my int $parent was being casted as a string. I’m presuming a new line character or something was being added to the var thus making php think it was a string.

Solution was then to cast the var $parent as an int like so: (int)$parent

So changing the last line to

wp_set_object_terms($id, (int)$parent, "warehouse");

Solved the problem 😉