Calling clean_term_cache() fails when called in the same plugin that creates terms, succeeds when called separately?

After creating about 40 test WP installs today on localhost, here’s what I can report:

It appears that the correct function call to rebuild the category_children options is:

_get_term_hierarchy('category')

Previously, I was trying to call:

clean_term_cache('','category')

And I was calling it just after my categories were created in my plugin code. However, I could never get this function to do anything when called from the same plugin that I’m using to create the categories. Interestingly, I could place the function in a stand-alone plugin and run it (after my category creation plugin) and it would rebuild the category_parents options array perfectly.

The reason it can’t be called in the same script that creates the categories appears to be related to a peculiar behavior of clean_term_cache() which is best stated by “foofy” in a comment in the bug track ticket:

This is only a problem if you make
multiple term changes in the same PHP
request/script lifetime.
clean_term_cache() tracks each
taxonomy it cleans in a static
variable and won’t clean it a second
time. If several term changes are made
in the same request, the
{$taxonomy}_children option is left
with the hierarchy as it was after the
first change.

I hope this helps someone!

Leave a Comment