Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following code

$genres= array('action', 'comedy', 'horror');
foreach($genres as $genre){
      $ret = wp_set_object_terms( $postId, $genre, 'genres');

But this code associates only horror as the genre. When I checked the DB too, I don't have a record for action and comedy. How do I associate all the three with my genre?

Thanks in advance.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You can pass an array of terms to wp_set_object_terms, there is no need for the for each:

$genres= array('action', 'comedy', 'horror');
$ret = wp_set_object_terms( $postId, $genres, 'genres');
share|improve this answer
Alternately, pass true as the fourth argument to wp_set_object_terms so that the term you pass is appended to, rather than overwriting, the existing terms. –  goldenapples Aug 23 '11 at 17:41
Thanks. It worked. –  Albin Joseph Aug 24 '11 at 15:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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