I'm looking for a better way to update a set of taxonomy terms. It's currently causing cron to hang which slows down the site substantially.
//Look through all of the stocks
foreach ($terms_data as $symbol => $data) {
//Check to see if term exists in the default vocabulary
$matching_terms = taxonomy_get_term_by_name($symbol, __DEFAULT_VOCAB_MACHINE_NAME);
//Loop through existing term(s)
foreach ($matching_terms as $term) {
//Load the new vocab term
$new_term_data = taxonomy_term_load($term->tid);
foreach ($data as $k => $v) {
//Add in the new data
$field_name = substr("field_".$k, 0, 31);
$new_term_data->{"$field_name"}['und'][0]['value'] = $v;
}
taxonomy_term_save($new_term_data);
}
} //endforeach
The only way I could figure out how to get it to work was to:
- Find the term that matches the $symbol variable
- Loop through the matching term (1 term)
- Create a new term
- Update the exact fields
- Save overtop of the old term
I know there has to be a better way to do this. Any ideas? Thanks in advance!