Support » Plugins » 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?


Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Mark Ratledge


    Forum Moderator

    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)

    //import categories from csv
    //sample data for $import_file category,category parent,slug,description
    //Parent categories must be in file before child categories
    //"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) {
      $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');

    Backup your database before attempting WordPress Backups

    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.

    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.