Title: Cannot Avoid Creating Duplicate Categories
Last modified: August 19, 2016

---

# Cannot Avoid Creating Duplicate Categories

 *  Resolved [WebTechGlobal](https://wordpress.org/support/users/webtechglobal/)
 * (@webtechglobal)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/cannot-avoid-creating-duplicate-categories/)
 * Hi,
 * For 2 days I’ve been working on a bit of code that automatically creates a bunch
   of categories from csv file. I used to work just fine until recently despite 
   no changes. Now for some reason the script creates the PARENT and CHILD plus 
   links a posts so all is ok.
 * Then on the next refresh of the blog the script creates a duplicate of the child
   category with the slug being extended by the second word of the first child category
   slug. A post is also put in that duplicate category.
 * Then after that all new posts are put in the first child category and the duplicate
   no longer matters. On a mass scale this is not funny so please understand I’m
   talking about csv importing plugins.
 * Here is my code and I would seriously appreciate anyone poking holes in it…
 *  $catid = get_cat_ID( $categoryname1 );// returns false if no match found else
   true
 *  // make new category if false returned
    if($catid == false) { if($catparent 
   == 1)// if = 1 all new categories have the set parent from options page { $my_cat
   = array(‘cat_name’ => $categoryname1, ‘category_parent’ => $catparent); if($catid
   == false){$catid = wp_insert_category( $my_cat );} } elseif($catparent == 0) {
   $my_cat = array(‘cat_name’ => $categoryname1); if($catid == false){$catid = wp_insert_category(
   $my_cat );} } }
 *  if(isset($catid)){$catidarray[‘0’] = $catid;}
 *  ############# CHILD ONE #################
    if( $cam->filtermethod== ‘automated’&&
   $cam->filtercolumn2 != 999) { // if category name does not exist create it
 *  $arg= array();
    $arg[‘child_of’] = $catid; $arg[‘hide_empty’] = false; $subcategories
   = get_categories( $arg );
 *  foreach ($subcategories as $cat)
    { if($cat->cat_name == $categoryname2) { $
   catid2 = $cat->term_id; } }
 *  // make new category if false returned
    if(!isset($catid2) || $catid2 == false){
   $my_cat = array(‘cat_name’ => $categoryname2, ‘category_parent’ => $catid); $
   catid2 = wp_insert_category( $my_cat ); } }
 *  if(isset($catid2)){$catidarray[‘1’] = $catid2;}
 * Obviously thats not the whole script but the main area is…
 * $subcategories = get_categories( $arg );
    foreach ($subcategories as $cat)
 * It is not finding the child for the parent and so allow the foreach loop to run
   and find that child to get the id.
 * Why?!
 * Parent ID is passed for sure else it would keep creating duplicates constantly.
 * Thanks everyone
    Ryan Bayne

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

 *  Thread Starter [WebTechGlobal](https://wordpress.org/support/users/webtechglobal/)
 * (@webtechglobal)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/cannot-avoid-creating-duplicate-categories/#post-1277739)
 * I can’t believe I just fixed it after debugging for another entire day and coming
   here for help!
 * Replace wp_insert_category with wp_create_category and it works better. I’m not
   going to look into why right now, but ye why would one cause the child of a parent
   to not be detected and so a duplicate is created BUT when that duplicate is created
   the initial child gets used for the new posts!
 *  [wydumanagement](https://wordpress.org/support/users/wydumanagement/)
 * (@wydumanagement)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/cannot-avoid-creating-duplicate-categories/#post-1278013)
 * Hey WebTech i just did a large merger from a blogger to a WP setup, whats the
   chances of getting a copy of the script to create a whole bunch of categories
   so i do not have to do it manually ??
 * its a long shot but i figured if i didn’t ask i wouldn’t know.
 * Thanks

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

The topic ‘Cannot Avoid Creating Duplicate Categories’ is closed to new replies.

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [child](https://wordpress.org/support/topic-tag/child/)
 * [duplicate](https://wordpress.org/support/topic-tag/duplicate/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [wydumanagement](https://wordpress.org/support/users/wydumanagement/)
 * Last activity: [16 years, 6 months ago](https://wordpress.org/support/topic/cannot-avoid-creating-duplicate-categories/#post-1278013)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
