Fixing WordPress category imports

Author: Lee Aplin

If you have to move a large site witha number of hierarchical categories from one WordPress installation to another, you may come across a problem we at Substrakt have faced quite a few times.

WordPress deals with hierarchical taxonomies like categories in a strange way. It not only stores the categories in the relevant taxonomy tables, along with parent IDs, it also stores an array of those parent-child relationships in the wp_options table.

The upshot of this is that, if you import a blog and you find that not all of the categories you’ve imported are appearing, fear not, chances are they’ve imported just fine, it’s just that the category_children option within wp_options hasn’t been updated.

When you setup a new site, you’ll probably find the value of this option is something like “a:0:{}”. Problem with that is that WordPress recognises that it’s an array of options, so doesn’t bother to correct it. If however, you clear that value out of your database – using something like phpMyAdmin or the below SQL statement – you should find the next time you get a list of categories, the hierarchy will be displayed correct.

UPDATE wp_options SET option_value = '' WHERE option_name = 'category_children';

This happens because WordPress first checks to see if that value is an array. If it is, as I’ve mentioned, it’ll take the value as read. If not – ie: if the value of that option is blank – it will rebuild the tree and resave the value. The next time you look at that value in your database, you should find something a bit more verbose.

Hope that helps. And if you’re having any WordPress issues at all, get in touch with us and we’ll see if we can help.