1

Why does this work to remove a single term?

$existingTerms = wp_get_post_terms($postID, 'custom_taxonomy');
        $newTerms = array();
        foreach($existingTerms as $term) {
            if ($term->slug != 'remove_this_term') {
                $newTerms[] = $term->term_id;
            }
        }
wp_set_post_terms($postID, $newTerms, 'custom_taxonomy');

But this doesn't?:

wp_remove_object_terms($postID, 'remove_this_term', 'custom_taxonomy');

Am I misunderstanding what wp_remove_object_terms does?

3
  • Should work as expected. Did you notice the two terms differ in the set and remove function? You added a s. So maybe you get early returned by wp_remove_object_terms. Commented Apr 1, 2018 at 8:19
  • That was just a test variable sorry. In my real code both are the same
    – Eckstein
    Commented Apr 1, 2018 at 13:47
  • 2
    If the term argument is a string it is interpreted as the term slug. Use integers for ids. Commented Apr 1, 2018 at 16:39

1 Answer 1

2

I know this is old but I ran across this issue, adding true to the end of the hook resolved the issue. In this instance wp_remove_object_terms($postID, 'remove_this_term', 'custom_taxonomy', true); will run the hook.

2
  • 1
    It doesn't have a fourth parameter. I believe their term_id was a string, not integer.
    – Christine Cooper
    Commented Dec 19, 2020 at 11:42
  • Just would like to add my two cents, even tho wp_remove_object_terms documentation state string|int|array $terms that isn't true. Only int are permitted when using ids. wp_remove_object_terms((int)$postID, (int)$termID, 'custom_taxonomy'); Commented Mar 23 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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