Codex tools: Log in
Adds a new term to the database. Optionally marks it as an alias of an existing term.
Error handling is assigned for the nonexistance 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.
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.
<?php wp_insert_term( $term, $taxonomy, $args = array() ); ?>
$parent_term = term_exists( 'fruits', 'product' ); // array is returned if taxonomy is given $parent_term_id = $parent_term['term_id']; // get numeric term id wp_insert_term( 'Apple', // the term 'product', // the taxonomy array( 'description'=> 'A yummy apple.', 'slug' => 'apple', 'parent'=> $parent_term_id ) );
This function calls five different hooks:
All five hooks are passed the term id and taxonomy id as parameters.
wp_insert_term() is located in