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 this really simple setup where I loop through an array of actors, and for each actor name I successfully insert a term. Then I use wp_set_post_terms to assign the terms to a post, but this only assigns the last term to that post. I am new to using taxonomy/terms so maybe there's an obvious solution. Here's my code:

foreach ($cast as $actor) {

    $actor_term_id = wp_insert_term( $actor[name], 'actor', array(
        'description' => $bio[0]["desc"]
        )
    );

    // Assign term id to post
    wp_set_post_terms( $post_id, $actor_term_id, 'actor' );

}
share|improve this question

1 Answer 1

up vote 1 down vote accepted

You need send a fourth argument to the function:

wp_set_post_terms($post_id, $actor_term_id, 'actor', true);

Each time the function executes, you're replacing any existing terms. The fourth argument specifies that you wish to append them.

https://codex.wordpress.org/Function_Reference/wp_set_post_terms

share|improve this answer
    
I see, thanks. However, I noticed an issue: I can only assign an actor term to one post this way. If I use the function in a loop that has an actor name that was already added and assigned to a post before, it won't assign that term to another post... –  Marc Sep 29 '12 at 18:40
    
I don't believe that this is an issue with WordPress, that's just how you chose to setup your loop. From WordPress' perspective, you can assign many terms to many posts. –  NightHawk Sep 29 '12 at 19:11

Your Answer

 
discard

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.