My plugin code is below. In the myPlugin_cleanup() function, I'm creating some categories and assigning term id 1 as the parent.

Due to the known bug with creating categories via script (the category_children array does not get created and results in a fatal error when pages containing category listings are accessed), I've placed the clean_term_cache() call directly below the function that creates my categories.

However, its not working. The category_children option is not getting created.

Interestingly, if I create a plugin whose sole function is to execute the clean_term_cache('','category') call, it fixes the problem and creates the category_children option with the correct values.

I'm just not sure why its not working in the same plugin that creates the categories.

What might I be missing?

function myPlugin_activate(){
    if ( get_option( 'myPlugin_activated' ) !== "1"){
        //comment out any function you don't want to run
        clean_term_cache('','category'); //does not work here
        update_option('myPlugin_activated', "1");

function myPlugin_cleanup(){

        //include dependencies


        clean_term_cache('','category'); //Does not appear to work here either.

register_activation_hook(__FILE__, 'myPlugin_activate');
  • Update: It appears that the fix for this is to call _get_term_hierarchy('category') NOT clean_term_cache('','category');
    – Scott B
    Commented Apr 19, 2011 at 21:34

2 Answers 2


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:


Previously, I was trying to call:


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!


I have to do this:

$taxnomy_name = ...;
clean_term_cache ( _get_term_hierarchy($taxnomy_name), $taxnomy_name);
  • 3
    Can you explain that?
    – fuxia
    Commented Oct 25, 2012 at 13:44

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.