Title: Adding categories via database
Last modified: August 19, 2016

---

# Adding categories via database

 *  [placutus](https://wordpress.org/support/users/placutus/)
 * (@placutus)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/adding-categories-via-database/)
 * Hello,
    I need to add a large number of categories. I feel it is going to be 
   very time consuming process if I try to do it via admin panel. I wonder if I 
   can do it by adding categories directly to the database. Would be only wp_terms
   and wp_term_taxonomy tables involved? Would be any other tables involved in the
   process?
 * If this operation — via database — is not possible, anybody knows about a way
   other than admin panel to add a large number of categories?
 * Thanks

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

 *  [Mark Ratledge](https://wordpress.org/support/users/songdogtech/)
 * (@songdogtech)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/adding-categories-via-database/#post-1418392)
 * Maybe [WordPress › Mass Custom Fields Manager « WordPress Plugins](http://wordpress.org/extend/plugins/mass-custom-fields-manager/)
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/adding-categories-via-database/#post-1418406)
 * Never made this into a plugin and haven’t used this in a whole, but put a csv
   file cat.txt in your theme folder and put this text in your theme’s index.php(
   or a page template) and just visit your blog (or page)
 *     ```
       <?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);
       }
       ?>
       ```
   
 * Backup your database before attempting [WordPress Backups](http://codex.wordpress.org/WordPress_Backups)
 *  Thread Starter [placutus](https://wordpress.org/support/users/placutus/)
 * (@placutus)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/adding-categories-via-database/#post-1418450)
 * MichaelH, unfortunately this code is not working. It doesn’t create any categories.
 * Would it be ok if I populate the tables **wp_terms** and **wp_term_taxonomy**
   by importing other text files?
 * Text file to import into **wp_terms** includes the following fields:
    `term_id`,`
   name`, `slug`, `term_group`.
 * Text file to import into **wp_term_taxonomy** includes the following fields:
   `
   term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`.
 * It would create categories and save a lot of time. The problem is about if the
   categories will work properly or if I have to involve other tables.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/adding-categories-via-database/#post-1418462)
 * In theory that could work, though don’t think you need ‘term_id’ in for the terms
   table or term_taxonomy_id for the term_taxonomy table as those are auto incrementing.

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

The topic ‘Adding categories via database’ is closed to new replies.

## Tags

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

 * 4 replies
 * 3 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [16 years, 2 months ago](https://wordpress.org/support/topic/adding-categories-via-database/#post-1418462)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
