I have a list of 350 or so terms that I am trying to set to a post. Here's my code:

add_action('init', 'add_cities_to_story');
function add_cities_to_story() { 
    $cities_list = get_term_children('115', 'location'); // MA
    wp_set_object_terms('632', $cities_list, 'location'); 

The trouble is that it appears to timeout about 3/4 of the way through the list.

Also, any ideas how to bulk remove the tags from a post? :)

  • You should be careful with wp_set_object_terms() because by default it will remove any existing terms on the object that aren't duplicated in the $terms argument. In this case you'd lose all locations not in $cities_list.
    – jerclarke
    Sep 9, 2011 at 21:25
  • FWIW I'd also propose that you revisit the decisions that lead you to want to add hundreds of terms to a single post. That is probably not the right way to go in terms of taxonomy design and if used regularly will give you a lot of headaches as your database grows over time.
    – jerclarke
    Sep 9, 2011 at 21:27
  • @Jeremy, thanks for the comment! My original goal was to get the "empty" terms to display on the front end by having a post associated with each term. It turns out I didn't need to do that. Sep 13, 2011 at 0:32

1 Answer 1


Dealing with terms is known to be very heavy operation.

The common tweak recommended is to use wp_defer_term_counting() to temporarily disable updating of terms count (which is stored persistently in database and so must be updated when you change terms).

  • Would I just include that to run in my function before I use wp_set_object_terms? Jul 18, 2011 at 21:23
  • @Jonathan turn off before, turn back on after
    – Rarst
    Jul 18, 2011 at 21:59

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.