How to add terms to taxonomy with wp_insert_terms?

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

wp_insert_term(
  'Glasgow', // the term 
  'location', // the taxonomy
  array(
    '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.

Leave a Comment