Languages: English • Español • 日本語 (Add your language)
Adds a new term to the database. Optionally marks it as an alias of an existing term.
<?php wp_insert_term( $term, $taxonomy, $args = array() ); ?>
The arguments decide how the term is handled based on the $args parameter. The following is a list of the available overrides and the defaults.
If 'slug' argument exists then the slug will be checked to see if it is not a valid term. If that check succeeds (it is not a valid term), then it is added and the term id is given. If it fails, then a check is made to whether the taxonomy is hierarchical and the parent argument is not empty. If the second check succeeds, the term will be inserted and the term id will be given. If the slug argument is empty, then it will be calculated from the term name.
Error handling is assigned for the nonexistence of the $taxonomy and $term parameters before inserting. If both the term id and taxonomy exist previously, then an array will be returned that contains the term id and the contents of what is returned. The keys of the array are 'term_id' and 'term_taxonomy_id' containing numeric values.
It is assumed that the term does not yet exist or the above will apply. The term will be first added to the term table and then related to the taxonomy if everything is well. If everything is correct, then several actions will be run prior to a filter and then several actions will be run after the filter is run.
$parent_term = term_exists( 'fruits', 'product' ); // array is returned if taxonomy is given wp_insert_term( 'Apple', // the term 'product', // the taxonomy array( 'description'=> 'A yummy apple.', 'slug' => 'apple', 'parent'=> $parent_term['term_id'] // get numeric term id ) );
This function calls these hooks with term id and taxonomy id as parameters:
Since: 2.3.0
wp_insert_term() is located in wp-includes/taxonomy.php
.