I have a huge excel database of locations which I want to add as terms to my current taxonomy.

However, I don't know the best way to do this, as inserting manually could take weeks.
I have looked at: http://codex.wordpress.org/Function_Reference/wp_insert_term
but not sure how to use it correctly.

My Taxonomy is called: location

I would like to add the following example terms:
London, Glasgow, Bristol, Bournemouth

Is this possible?

Additionally, if there is hierarchical relationships, is this also possible to add automatically?

For instance: London (Parent), Kensington (child of london).

1 Answer 1


You'd write a script to loop through a file you created with the data, and process each line one at a time. Inside the loop you'd have code like the following. You would, of course, replace my example values Scotland, Glasgow, its description and its slug with variables representing the data for the row being processed.

$parent_term = term_exists( 'Scotland', 'location' );  // find parent term
$parent_term_id = $parent_term['term_id']; // get numeric term id

  'Glasgow', // the term 
  'location', // the taxonomy
    'description'=> 'Describe Glasgow.',
    'slug' => 'glasgow',                   // what to use in the url for term archive
    'parent'=> $parent_term_id

This commercial plugin GD Custom Posts And Taxonomies Tools includes a feature to import / export taxonomy tags. It might be worth the cost, depending on how you value your time.

  • Hi Marfarma, Thanks for replying. I forgot to mention that i am using a custom post type (called real_estate) not sure if that makes a difference? Do i just add this code to the functions.php? Thanks in advance.
    – PaulB
    Aug 22, 2012 at 7:14
  • It doesn't make any difference that you have a custom post type. @PaulB - with respect, if you don't understand this enough to know that the sample code would be put into a larger stand-alone php script or custom plugin, you're much better off buying the commercial plugin I linked. Saving 44 EUR can't be worth the time it would take you to figure this out - never mind the other features you'd be able to use in the future.
    – marfarma
    Aug 23, 2012 at 13:23
  • @marfarma I'm using some similar code. The codex states that wp_insert_term() should run on add_action('init'). How would I run it only once on plugin activation?
    – codecowboy
    Oct 23, 2013 at 16:09
  • Use a plugin activation hook and add the add_action('init') and insert the terms in the function you write and assign to that hook. Please note, that this is an 'off the cuff' answer, and that I'm not actively coding WordPress at the moment, so I won't be able to follow up with a more complete answer. The cognitive costs of task switching are a bit too high to pay at the moment.
    – marfarma
    Oct 24, 2013 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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