I have a function that runs with user input variable (comma separated numeric string) to update the terms (by id) in a custom taxonomy on a custom post type. Even though the docs say I should use wp_set_object_terms, I can only get my terms to update by using wp_set_post_terms. The following code will work (using wp_set_post_terms but not using wp_set_object_terms at the end):

if(isset($request['custom_tax'])) {
$customtaxarray = explode(",",$request['custom_tax']);
$only_integers = true;
foreach ($customtaxarray as $testcase) {
    if (!ctype_digit($testcase)) {
    $only_integers = false;
if ($only_integers) {
$customtax = $customtaxarray;
} else {

return array(
                'code' => 'missing_integers',
                'data' => array(
                    'status' => 403,
                    'message' => "custom_tax must be one or more (comma separated) integers.",
//update custom_tax
wp_set_post_terms($request['cpt_post_id'], $customtax, 'custom_tax' );


After much trial and error, I have found a solution that will allow wp_set_object_terms to be used. Despite already checking if my strings were integers, apparently I needed to convert the array explicitly as well. So I changed this:

$customtax = $customtaxarray;

to this:

$customtax = array_map('intval',$customtaxarray);

and suddenly wp_set_object_terms now works. That does not explain why wp_set_post_terms was working instead (unless perhaps it does this conversion automatically?), but at least this is now working as expected.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.