Wordpress - How to add a default item to a custom taxonomy?

Have a look here:

https://web.archive.org/web/20150403012347/http://wordpress.mfields.org/2010/set-default-terms-for-your-custom-taxonomies-in-wordpress-3-0/

Basically what you need to do is use the save_post hook to check the terms for the post and add the default term from your taxonomy if it's empty.

If you just want to have an initial term set in your custom taxonomy, then you can use wp_insert_term(). Probably easiest to add it in the same function that you're using to create your custom taxonomy. As t3ios adds in the comments, you should call get_term() first, and only insert the term if the return value is null (ie the term doesn't exist).

This example code is from the Codex: http://codex.wordpress.org/Function_Reference/wp_insert_term

$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
  )
);

Default category is hardcoded case in wp_insert_post() function.

So it can't be replicated exactly, but you can handle it in other ways. I'd try to hook into post status transition for new posts and assign wanted default term if none were assigned during post creation.

Tags:

Taxonomy