WordPress.org

Ready to get started?Download WordPress

Forums

Multiple / Batch category creation? - import categories from csv (5 posts)

  1. monkeyfight
    Member
    Posted 4 years ago #

    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

  2. MichaelH
    Member
    Posted 4 years ago #

    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);
    }
    ?>
  3. abiswift92
    Member
    Posted 4 years ago #

    CSV 2 POST can do this and it does it really well

  4. cordoval
    Member
    Posted 4 years ago #

    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).

  5. cordoval
    Member
    Posted 4 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic