I have added custom taxonomy to media attachments. From the individual post page these are updating correctly, with some tweaks to get the column count updating correctly - and I can see these changes in the admin/database without a problem.

In the media popup, I can change the taxonomy and using the edit_attachment action I can see wp_set_object terms is firing, I can also see that it returns an array with the correct IDs in for the updated term:

wp_set_object_terms($id, $cat_ids, 'attachment_category', false);
array(1) {
    string(1) "3"

3 being the correct ID for the term in question, however I cannot see an update in the database and when I refresh the page or close/reopen the popup the change is reverted.

Why is wp_set_object_terms in this case not updating the database but thinking it is successful? edit_attachment is the only action which correctly fired for me when the save-attachment-compat action is triggered on the form. If I add a die(); at the end of my function, and stop any further changes, it updates the database, is this the correct behaviour?

1 Answer 1


Before using $cat_ids in the wp_set_object_terms() function, cast all values from the string to a number.

Note from the documentation:

Integers are interpreted as tag IDs. Warning: some functions may return term_ids as strings which will be interpreted as slugs consisting of numeric characters!

  • Yes that's already done, cat_ids values are IDs, explicitly set with (int)$value, and the wp_set_object_terms fires correctly, but gets overwritten without a die() - which is the question, why does it need the die() ?
    – Aravona
    Jun 12, 2019 at 11:21

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.