WordPress.org

Ready to get started?Download WordPress

Forums

Adding categories via database (5 posts)

  1. placutus
    Member
    Posted 4 years ago #

    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

  2. MichaelH
    Member
    Posted 4 years ago #

    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

  3. placutus
    Member
    Posted 4 years ago #

    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.

  4. MichaelH
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags