0

I have a 1000+ terms I need to add to a taxonomy called county. I am trying to use this function wp_insert_term but the term is not being added to the database. I am running this code inside function.php as a test before looping over each unique term. Should I be using a specific hook?

wp_insert_term(
  'Maricopa',
  'county',
  array(
    'description'=> 'Maricopa',
    'slug' => 'maricopa'
  )
);

Also curious of other ways of adding multiple terms at once. Is there a REST API option for doing this?

EDIT

Tried using this hook but same result the term was not added to the 'county' taxonomy.

function add_county_terms() {
  wp_insert_term( 'Maricopa', 'county', array(
    'slug' => 'maricopa',
  ));
}
add_action( 'wp_loaded', 'add_county_terms' );
  • Does it add no terms? Or only some of them? Keep in mind that adding 1000 of anything will be slow, it's highly unlikely it will be finished before the PHP execution time limit kicks in and your request is stopped in its tracks. This is the kind of thing a WP CLI command is great at, or doing it bit by bit with the REST API – Tom J Nowell Dec 3 '18 at 1:27
  • As an aside, do you have 1000 wp_insert_term calls in your functions.php? How big is that file? – Tom J Nowell Dec 3 '18 at 1:27
  • For now only trying to add a single term 'Maricopa'. The plan is if I am able to add the one correctly run a foreach on my 'Counties' array using 'wp_insert_term' for each term in the loop. – Benjamin Dec 3 '18 at 1:41
  • 2
    Your taxonomy isn't registered until init, so that code definitely needs to run later than when the file is loaded. – Milo Dec 3 '18 at 1:56
  • Does it return any error? – Krzysiek Dróżdż Dec 3 '18 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.