Title: Programmatically adding WP categories in plugin.
Last modified: August 21, 2016

---

# Programmatically adding WP categories in plugin.

 *  [wp_joost](https://wordpress.org/support/users/wp_joost/)
 * (@wp_joost)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/programmatically-adding-wp-categories-in-plugin/)
 * Hello all!
 * I am in the process of making a plugin that loads external XML-feeds, combining
   them, extracting all categories, normalising them manually and than adding them
   to WordPress.
 * What i get after normalising is this array :
 *     ```
       $parent_categories = Array
       (
           [targetaudience] => Array
               (
                   [Baby boys] =>
                   [Baby girls] =>
                   [Boys] => Boys
                   [Boy] => Boys
                   [Girls] => Girls
                   [Girl] => Girls
                   [Unisex] =>
               )
   
           [productgroup] => Array
               (
                   [Pants short] => Pants
                   [Pants long] => Pants
                   [Trousers] => Pants
                   [T-shirt] => T-shirts
                   [Shirt] => T-shirts
                   [Shirts] => T-shirts
                   [T-shirt long-sleeve] => T-shirts
               )
       );
       ```
   
 * Basicly, on the left side (array key) is the old name, on the right side (array
   value) is the new name, when there is no new name, the old name will be used.
   
   Sound simple right? But when i start adding the categories either with wp_create_category
   or wp_insert_category it does something strange.
 * It add’s all the categories but when i go to the WP categories page it says 372
   items but only the parent-categories are visable (productgroup and targetaudience)
   I checked the database and all categories are added like expected and i couldn’t
   find any missing links or something.
 * Only when i delete or edit a category, the child-categories show up.
 * This is the code i use to add the categories :
 *     ```
       foreach($parent_categories as $parent_name => $child_categories) {
   
       		$parent_id = wp_create_category($parent_name);
       		}
       		//If parent-category was created successfully
       		if($parent_id!=0) {
       			foreach($child_categories as $old_cat_name => $new_cat_name) {
       				if($new_cat_name=='') {
       					//Use old name
       					$id = wp_create_category($old_cat_name, $parent_id);
   
       				} else {
       					//Use new name
       					$id = wp_create_category($new_cat_name, $parent_id);
       				}
       			}
       		}
       	}
       ```
   
 * I really have no idea why this is happening, i’ve heard of other people having
   problems with wp_create_category but i still haven’t found a solution.

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [wp_joost](https://wordpress.org/support/users/wp_joost/)
 * (@wp_joost)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/programmatically-adding-wp-categories-in-plugin/#post-4624118)
 * I noticed i might have placed this post in the wrong section, sorry for that.
   
   I also wanted to make clear that the array is **way** bigger in the real deal,
   currently i’m using 4 parent-categories with a total of +- 370 child-categories.
   Also, sometimes the last parent-category isn’t even added, it seems random, sometimes
   it’s added, sometimes it’s not.
 *  Thread Starter [wp_joost](https://wordpress.org/support/users/wp_joost/)
 * (@wp_joost)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/programmatically-adding-wp-categories-in-plugin/#post-4624235)
 * After a week of pulling my hair out i finally found a solution, for those of 
   you who are having the same problem, the problem is with the categories cache,
   it’s not updated after adding categories. The following snippet fixes the problem:
   delete_option(‘category_children’);

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Programmatically adding WP categories in plugin.’ is closed to new replies.

 * 2 replies
 * 1 participant
 * Last reply from: [wp_joost](https://wordpress.org/support/users/wp_joost/)
 * Last activity: [12 years, 3 months ago](https://wordpress.org/support/topic/programmatically-adding-wp-categories-in-plugin/#post-4624235)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
