WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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? :)

share|improve this question
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. – jeremyclarke Sep 9 '11 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. – jeremyclarke Sep 9 '11 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. – Jonathan Wold Sep 13 '11 at 0:32
up vote 1 down vote accepted

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).

share|improve this answer
Would I just include that to run in my function before I use wp_set_object_terms? – Jonathan Wold Jul 18 '11 at 21:23
@Jonathan turn off before, turn back on after – Rarst Jul 18 '11 at 21:59

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.