0

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' );

}
2

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

| 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

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.