0

I am calling get_terms() in a loop. I may modify the terms (add one) in an iteration of the loop. I need to be sure that the next iteration gets this added one. Specifically; I test for a child term of a parent. If it does not exist I add it and use it. If if does exist I use it. So; if I add it on the first iteration, say, I need to get it on the next iteration when I call get_terms. In pseudo-code:

for ($posts as $post_id)
{

    $term_id = wp_get_object_terms($post_id); //just one
    $child_terms = get_terms(array('parent' => $term_id));
    if (count($child_terms) < 1)
    {
        //create new term (based on the term of $term_id and add it to post
        $new_term_id = wp_insert_term('name', 'mytax', array('parent' => $term_id));
        wp_set_object_terms($post_id, $new_term_id);
    } 
    else
    {
        //assign $child_term :
         wp_set_object_terms($post_id, $child_terms[0]->term_id);
    }

} 

(I'm using pseudo-code because the whole thing is rather complicated; but this shows the flow).

I think that I should clear the terms cache at the top of the loop. I have found clean_taxonomy_cache() and wp_cache_flush() - the latter seems to clear the whole query cache? Which is better? Will clean_taxonomy_cache() do it? Is there anything else?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.