Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to insert hundreds of categories, structured hierarchically. Any suggestions on how I could do it in a batch mode? Thanks.

share|improve this question
    
I did it with a json file recently. I created a page with a custom template that call a function who call the JSON file, parse it, and create each category recursively (testing first if the category already exists). I could provide code if this interesting for you. –  Simon Sep 4 '12 at 18:41
    
Sorry, it wasn't a JSON file, but a simple print_r of an array ! But it would be nicer to use JSON ^^ –  Simon Sep 4 '12 at 19:11
    
Yes I would appreciate this very much. Please do provide such code. Thanks! –  p.a. Sep 4 '12 at 19:46
    
Did you find it useful ? –  Simon Sep 12 '12 at 12:10
    
@Simon This is very useful code. I still work on it and use it in several occasiotions. However I opted on inserting my categories manually. –  p.a. Sep 13 '12 at 10:17
add comment

2 Answers 2

This is main function of the import process.

function menu_recursive_output( $branch_objet, $parent_id, $level ) {

// JUST FOR COUNTING ITEMS
static $counter = 1;

if ( $level ) {

    // CHECK IF ITEM EXISTS ALREADY
    // IF SO, THIS WILL RETURN ARRAY CONTAINING ID OF EXISTING ITEM
    $created_cat = term_exists( $branch_objet->name, 'category', $parent_id);   

    // IF NOT, WE CREATE ITEMS
    // WITH 
    if ( ! $created_cat ) {

    $created_cat = wp_insert_term( $branch_objet->name, 'category', array(
        'description'=> $parent_id,
        'slug'=> $branch_objet->id . '_' . $branch_objet->slug,
        'parent'=> $parent_id
    ));

    } else {

    $created_cat = wp_update_term( $created_cat['term_id'], 'category', array(
        'description'=> $parent_id . 'updated!',
        'slug'=> $branch_objet->id . '_' . $branch_objet->slug,
        'parent'=> $parent_id
    ));

    }
}
//if ( is_wp_error( $created_cat ) ) echo $created_cat->get_error_message();
// ECHOING DATA JUST TO VISUAL CHECK
echo $counter; $counter++;
for ( $i = 0; $i < $level ; $i++) echo '--';
echo '|';
echo '<a href="' . get_category_link($created_cat['term_id']) . '">' . $branch_objet->name . '</a> | id = ' . $created_cat['term_id'] . ' | parent = ' . $parent_id . ' | level = ' . $level . '<br />';

// $options_categories[ $created_cat['term_id'] ] = $branch_objet->name;

// IF CHILDREN START A NEW RECURSION WITH EACH CHILD
// WITH CATEGORY ID AS PARENT ID
if ( $branch_objet->children != '-' ) {
    foreach( $branch_objet->children as $sub_branch ) {
        menu_recursive_output( $sub_branch, $created_cat['term_id'], $level + 1 );
    }
}

}

First I need a object that contains the category. In this case, it should be a object, but could be easily adapted to deal with an array.

I start the process with providing the object and start a level 0 :

// START RECURSION 
menu_recursive_output ( $cat_object, 0, 0 );

You could easily change it create a custom taxonomy by changing 'category' in wp_insert_termand wp_update_term functions.

In my case, I had for each category, and ID and a slug provided, because I was importing from another database. I don't know your specific, so you may need to adapt it.

Hope this could help, feel free to ask and comment.

EDIT TO ADD CONTEXT INFORMATION :

I had used this function in a template file, instead of the regular loop, so I just lauch the import process by displaying a particular page of my page and I output the data with echo so I have an instant and clear view of the structure of my categories, see if it looks OK.

As you see, the function also could update data so if you change something in your categories file (name for example), you just need to run it again and data will be update.

What it does NOT do is modifying structure : if you delete a category from the imported file, it will not be deleted on the WP structure. It does not compare data, just parse it, and for each item create it if not present, update it if present.

To start testing, I just changed one line :

if ( $level ) {

by

if ( $level < 1 ) {

so the import stops right after the first level (parents) categories. If everything's fine at this point (name, hierarchy) you could remove the conditionnal number.

To import data i just use :

$file_to_import = file_get_contents( TEMPLATEPATH . '/import/arbo.php' );
share|improve this answer
add comment

Would this do the trick?

http://wordpress.org/extend/plugins/category-import/

share|improve this answer
    
Thanks, I tried it and its very unstable; sometimes it works as intended sometimes not. –  p.a. Sep 1 '12 at 4:22
    
I recommend you are very cautious with category-import. Try injecting 2-3 categories, verify the result and then try adding more. This widget messed my categories-taxonomy data and I cant clear them out now.. Take a backup in advance! –  p.a. Sep 5 '12 at 6:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.