Importing a CSV file into WordPress Categories

By North Street, A Creative Studio

You’ll need to adjust this code, but this simple script takes a CSV file, and import the data into WordPress Categories.

  1. In order for this script to work correctly, the CSV file must be in order by $parent_id — That is, the top-most level parents must come first in the CSV file.
  2. Each $line corresponds to a line of the CSV file. You’ll need to adjust the array indexes to match the columns in the CSV file you currently have.
  3. WordPress automatically creates the new category IDs. This script keeps track of the new and old category IDs, in order to make sure the parent relationship stays intact.
  4. When the script is complete, it spits out the old to new category id conversion table for you save, if you need that for further importing of data.

That’s basically it.

<?php echo "Let's import some cats!"; 

$data_file = file('test.csv');

$convert_table = array(); // old id => new id

foreach ($data_file as $data) {
	$line = explode(',', $data);

	print_arr($line);

	$old_id = $line[0];
	$old_parent_id = $line[2];
	//$term = substr($line[3],1,-1);
	$term = $line[3];

	$desc = strip_tags($line[17]);

	// Figure out parent id
	if ($old_parent_id != 0) {
		$parent_id = $convert_table[$old_parent_id];
	}
	else $parent_id = 0;

	echo "Attempting to insert category:";
	$return = wp_insert_term(
	  $term, // the term 
	  'category', // the taxonomy
	  array(
	    'parent'=> $parent_id,
	    'description'=> $desc
	  )
	);

	if (is_array($return)) {
		$new_id = $return['term_id'];

		//Add to $convert_table
		$convert_table[$old_id] = $new_id;

	}

	print_arr($return);

}

echo "All Done. Save this conversion table!";

print_arr($convert_table);

Here’s a sample of the CSV file used for this particular script, though remember that depending on where your data is coming from, your CSV may be different. This was from a CakePHP build and has many columns that I didn’t need.

Please also note that my script assumes the first row is removed from the CSV file (I’ve left it in below to make it more clear.) Finally, to sort by the parent_id, I just brought the CSV file into Google Sheets.

id,user_id,parent_id,"title","status",rank,"created","modified",associate_documents,associate_posts,associate_forms,associate_calendars,associate_users,associate_organizations,associate_contact_prefs,associate_admin_only,"thumbnail","description"
89,51,0,"Sort by Event Type","publish",22,"2007-06-26 14:56:53","2012-04-19 10:17:11",1,1,0,1,0,0,0,0,"",""
88,54,80,"Visual Arts","publish",10,"2007-06-23 18:28:56","2007-08-07 15:27:16",1,1,1,0,1,0,0,0,"",""
84,25125,93,"Folk / Traditional Arts","publish",25,"2007-06-23 18:26:40","2012-07-19 16:55:23",1,1,0,0,1,0,0,0,"",""
85,54,80,"Literary Arts","publish",5,"2007-06-23 18:27:24","2007-08-07 15:27:16",1,1,1,0,1,0,0,0,"",""

 

Extra Credit: The imported categories will be ordered by the time they were imported….resulting in a reverse-alphabetical order.

To fix that, add this to functions.php

/* Let's order the categories alphabetically in the admin.
http://wordpress.stackexchange.com/questions/53094/how-to-change-the-categories-order-in-the-admin-dashboard */
add_filter( 'get_terms_args', 'ns_sort_get_terms_args', 10, 2 );
function ns_sort_get_terms_args( $args, $taxonomies ) 
{
    global $pagenow;
    if( !is_admin() || ('post.php' != $pagenow && 'post-new.php' != $pagenow) ) 
        return $args;

    $args['orderby'] = 'slug';
    $args['order'] = 'ASC';

    return $args;
}

 

About north street

We engineer the thoughtful transformation of great organizations. Our proven process helps us understand what your competitors are doing right — and wrong. Want to learn more? Let’s chat.

More Notes

A bowler hat with radio waves behind it

From Layoff to Leadership: Tom Conlon’s Journey to a Prosperous Business

Welcome to Your 2024 Branding Pep Talk

A bowler hat with radio waves behind it

CEO Tom Conlon talks shop on Podcast Marketing Secrets