WordPress.org

Ready to get started?Download WordPress

Forums

Bulk Category Upload (6 posts)

  1. Net Zero
    Member
    Posted 3 years ago #

    Hey,

    have tried for several hours to find a solution (in forums and myself) for this with no avail.

    I have a spreadsheet with states, counties and cities. Now I would like to import all the date into the database as categories.

    The categories (cities/counties/states) (if possible) should automatically be children of the other categories.

    Is there any way to do this?

    Thanks a lot and dont hesitate to ask questions...

  2. stvwlf
    Member
    Posted 3 years ago #

    Hi

    Did you see this code from MichaelH?

    http://wordpress.org/support/topic/multiple-batch-category-creation?replies=5#post-1110996

    Try it first on a development version of WP if you have the capacity to create one. It seems to recognize parent categories too.

  3. Net Zero
    Member
    Posted 3 years ago #

    Thanks. That is exactly what I was looking for. Unfortunately, it does not work.

    I created a small plugin that executes his code uppon activation. However, no cats get imported...

    Any other suggestions?

  4. stvwlf
    Member
    Posted 3 years ago #

    Well, since you can write a plugin, you should put some debugging and tracing code into it to find out what is not working.

    I didn't look at the code much but I don't think much if anything would have changed as far as inserting categories into WP. That's pretty basic stuff. If you can find out where its malfunctioning you can get it fixed.

    You're not going to fund much else that close to what you are looking for.

  5. Net Zero
    Member
    Posted 3 years ago #

    Here the code I used:

    <?php
    /*
    Plugin Name: City Importer
    Plugin URI:
    Description: Imports csv-file with states/counties/cities as categories
    Version: 0.1
    Author: MichaelH & HendrikH
    Author URI:
    //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"
    
    function import_cats();
    {
             require_once(ABSPATH . "wp-admin/includes/admin.php");
             $import_file = 'cat.csv';
             $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);
    }
    
    register_activation_hook( __FILE__, 'import_cats' );
    
    }
    ?>

    My csv file is placed directly in the plugin folder.

  6. Net Zero
    Member
    Posted 3 years ago #

    I solved this by just using wp import / export function: Had a look at how the categories are exported. Then I used word's letter function to create a file with all my categories. Copy & Pasted this into the file and imported it. Works very nice.

    I suggest uploading the parent cats first, then their children, then their children, etc

Topic Closed

This topic has been closed to new replies.

About this Topic