The following snippet adds WordPress categories programmatically. My question is how do you add tags programmatically?
//Define the category
$my_cat = array('cat_name' => 'My Category', 'category_description' => 'A Cool Category', 'category_nicename' => 'category-slug', 'category_parent' => '');
// Create the category
$my_cat_id = wp_insert_category($my_cat);
In this question, I’m talking about adding the tags into database programmatically.
Say, I got 1000 tags to add to a fresh installation. And I don’t want to go through the regular admin panel to add tags one by one manually. I’m looking for a programmatic solution. The snippet I posted takes care of the add of cats… thanks to the specific wp function wp_insert_category…. there is no function called wp_insert_tag though…
However, looking at the codex, I see the wp_insert_term function which may very well be the one to do the job – it seems.
Use
wp_insert_term()
to add categories, tags and other taxonomies, becausewp_insert_category()
fires a PHP error “Undefined function”.$term
is the term to add or update.Change the value of
$taxonomy
intopost_tag
if it’s a tag andcategory
if it’s a category.In
$args
array, you can specify values of inserted term (tag, category etc.)Example: