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.
The topic ‘Programmatically adding WP categories in plugin.’ is closed to new replies.