If you’ve ever created a post in WordPress, you’ll know that you have to put it in a category. There’s no way around this, if you try to fight it, you will lose! On the other hand, there is really no way to define a default for custom taxonomies. I created the following code to enable you to set default terms for each taxonomy registered on your installation.
/**
* Define default terms for custom taxonomies in WordPress 3.0.1
*
* @author Michael Fields http://wordpress.mfields.org/
* @props John P. Bloch http://www.johnpbloch.com/
*
* @since 2010-09-13
* @alter 2010-09-14
*
* @license GPLv2
*/
function mfields_set_default_object_terms( $post_id, $post ) {
if ( 'publish' === $post->post_status ) {
$defaults = array(
'post_tag' => array( 'taco', 'banana' ),
'monkey-faces' => array( 'see-no-evil' ),
);
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $post_id, $taxonomy );
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
}
}
}
}
add_action( 'save_post', 'mfields_set_default_object_terms', 100, 2 );
You will need to configure the $defaults array to reflect your wishes. The keys are taxonomy slugs and the values consist of an array that contains term slugs from the taxonomy referenced in the key. You can list as many terms as you want to be set by default for each taxonomy.
This worked for me on a very plain installation of WordPress 3.0.1. If you know of an alternative method, or think that this code can be improved in any way, please leave a comment.



Hi Michael,
Great little snippet! I’ll have to use that in the future. Brilliant!
One thing: the “save_post” action sends two arguments to the callback: the ID and the post object. So you could remove the globalized $post variable and just put it into the arguments:
function mfields_set_default_object_terms( $id, $post ){$taxonomies ...
Thanks John! Should definitely come in handy in the future for sure. Your corrections were spot on and I’ve modified the code to reflect them. I guess this is what happens when I don’t do all of my homework :)
Useful! Thx may come in handy soon.
Cheers!
Thank you so much for this! It’s going to come in handy! :)
Im going to try this right now.
I have been researching this for 2 hours now, and started to think I was crazy.
Will update after I give it a go!
James Tryon
Yep! Worked Great ;c )
The only thing i had to change was the array like you said and changed $post_id, $post to $id, $post .
Why I needed to set the default tears:
Im working on a plugin for sponsors. There is a widget that goes along with the main custom post type. It calls the slug of one of the cat’s for show casing.
Thank you so much for sharing :c )
James Tryon, Creative Director
Easily Amused, Inc.
Thanks John, That did the trick!
Excellent! Works great with my custom taxonomies. This is exactly what I needed – thanks so much for sharing!
One more voice in the chorus: I’ve been trying to figure out how to do this for quite a while, and your code was just the ticket. Thanks for sharing it.
Absolutely fantastic little function, thank you for sharing. I’m using this for a menu for which ingredients need to plug into a dish post type. Adding an “Uncategorized” term for ingredients was therefore essential.
I have a created a custom post type called ‘devices’ and added a custom taxonomy called ‘status’ which has four terms ‘Broken,Fixed ,Under Repair and Waiting’ . now need to set the default term as ‘Broken’.Some one help me out please. Thanks in advance