I have a parent category named "Europe" under which I need to insert some 20+ child categories (France, Italy, Germany, etc.)

What is the SQL statement I have to fire to add - for example - France to Europe(category ID=1)?

This is 100% new blog, it has no posts, no custom fields, nothing.

1 Answer 1


Insert taxonomy terms via a raw SQL query is hard and discouraged, use the core wp_insert_term function, instead.

Assuming you have an array of continent/countries terms:

$countries_list = array(
  'Europe' => array(
    'Austria', 'France', 'Italy', 'Germany', 'Spain', 'United Kingdom'

foreach ( $countries_list as $continent => $countries ) {
  // get term object for continent
  $parent = get_term_by( 'slug', sanitize_title( $continent ), 'category'  );
  if ( empty( $parent ) ) {
    // continent term doesn't exist, let's create it
    $parent_a = wp_insert_term( $continent, 'category' );
    if ( ! is_array( $parent_a ) ) continue;
    // get term object for the just created continent term
    $parent = get_term( $parent_a->term_id );
  // loop countries to add the related terms
  foreach ( $countries as $country ) {
    // do nothing for current country if the related term already exists
    // see http://codex.wordpress.org/Function_Reference/term_exists
    if ( term_exists( sanitize_title( $country ), 'category', $parent->term_id ) ) {
    // insert the country term using continent as parent
    wp_insert_term( $country, 'category', array( 'parent' => $parent->term_id ) );

Of course, this is a very resource-expensive code, and even if code does checks to avoid terms are added more than once, you should be sure this code run only once, to avoid serious performance issues.

To setup a run-once script, have a look at this Q/A.

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.