How to add terms to taxonomy with wp_insert_terms?

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