Title: Multiple / Batch category creation? &#8211; import categories from csv
Last modified: August 19, 2016

---

# Multiple / Batch category creation? – import categories from csv

 *  [monkeyfight](https://wordpress.org/support/users/monkeyfight/)
 * (@monkeyfight)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/multiple-batch-category-creation/)
 * Hi. I’ve built a new site for someone and the way it needs to be structured required
   it to have hundreds of categories, maybe over 1000. Basically it’s UK Counties
   as the top level categories, then in each County is a category for each city.
 * I can add them all by hand but it may take a while. Is there an easy way, an 
   a plugin or a hack, to add multiple categories all at once rather than one at
   a time, clicking ‘OK’ after I’ve created each one?
 * Something like. For exacmple camma seperated entry?…
 * “Cambridge, Wisbech, Ely, March, Whittlesey, Chatteris, Linton” > Select Parent
   Category to ‘Cambridgeshire’, click OK and I’ve 7 new sub-categories created 
   for the price of one. Anything like that out there? I’ve not found anything but
   maybe I’ve just not looked in the right places 🙂
 * If anyones any ideas please let me know!
 * Thanks a lot,
    Rich

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/multiple-batch-category-creation/#post-1115923)
 * Here’s an ugly bit of code. Backup your database before using. Take two aspirin.
 *     ```
       <?php
       //import categories from csv
       //sample data for $import_file category,category parent,slug,description
       //Parent categories must be in file before child categories
       //
       //"county1",""
       //"county2",""
       //"city1","county1"
       //"city2","county2"
       //"city3","county1"
       //"county3",""
       //"County Cork","","county cork","county cork descripiton of this cat"
       //"County Morefields","","","This is description of county morefields"
   
       require_once(ABSPATH . "wp-admin/includes/admin.php");
       $import_file = TEMPLATEPATH . '/cat.txt';
       $import = array();
       $fh = fopen($import_file,'r');
       while($t = fgetcsv($fh)) {
           $import[] = $t;
       }
   
       foreach ($import as $importcat) {
         $numfields=count($importcat);
         $cat_ID = get_cat_ID($importcat[0]);
         $cat_name = $importcat[0];
         $category_parent = get_cat_ID($importcat[1]);
   
         if ($numfields > 2)
           $category_nicename = sanitize_title($importcat[2]);
   
         if ($numfields > 3)
           $category_description = $importcat[3];
   
         $args = compact('cat_ID', 'cat_name', 'category_description', 'category_nicename', 'category_parent');
         wp_insert_category($args);
       }
       ?>
       ```
   
 *  [abiswift92](https://wordpress.org/support/users/abiswift92/)
 * (@abiswift92)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/multiple-batch-category-creation/#post-1116295)
 * CSV 2 POST can do this and it does it really well
 *  [cordoval](https://wordpress.org/support/users/cordoval/)
 * (@cordoval)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/multiple-batch-category-creation/#post-1116333)
 * this is not true, cvs 2 post does not support importing or automatic creation
   of multiple categories or a hierarchy of categories. Code is simple and can be
   tweaked however, I am still working on it (not the developer just a modifier).
 *  [cordoval](https://wordpress.org/support/users/cordoval/)
 * (@cordoval)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/multiple-batch-category-creation/#post-1116334)
 * Solved it another way, i had to code within the taxonomy.php file and add my 
   own list of categories, when a category of level 2 is created then I create through
   code the other list of categories automatically. This fits my needs. If you want
   me to code something to fit your needs please let me know, of course at a charge,
   but I can do it since I am familiar with the taxonomy now. Thanks at cordoval
   at g m a i l dot com

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

The topic ‘Multiple / Batch category creation? – import categories from csv’ is 
closed to new replies.

## Tags

 * [meh_code](https://wordpress.org/support/topic-tag/meh_code/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 4 participants
 * Last reply from: [cordoval](https://wordpress.org/support/users/cordoval/)
 * Last activity: [16 years, 2 months ago](https://wordpress.org/support/topic/multiple-batch-category-creation/#post-1116334)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
