Set Default Terms for your Custom Taxonomies

Posted on

By Michael Fields

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.

11 Comments Comments are closed

  1. John P. Bloch September 14, 2010 at 4:14 pm

    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 ...

  2. Michael Fields September 14, 2010 at 4:26 pm

    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 :)

  3. Chuck Reynolds September 15, 2010 at 8:42 am

    Useful! Thx may come in handy soon.

    Cheers!

  4. Shane October 3, 2010 at 9:54 pm

    Thank you so much for this! It’s going to come in handy! :)

  5. James Tryon December 9, 2010 at 5:04 pm

    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

  6. James Tryon December 9, 2010 at 7:49 pm

    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.

  7. James Tryon December 9, 2010 at 7:51 pm

    Thanks John, That did the trick!

  8. Phillip Bryan December 13, 2010 at 1:06 am

    Excellent! Works great with my custom taxonomies. This is exactly what I needed – thanks so much for sharing!

  9. Paul Foth December 15, 2010 at 4:42 pm

    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.

  10. John Sparrow October 25, 2011 at 10:02 pm

    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.

  11. logesh November 2, 2011 at 5:49 am

    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

Fork me on GitHub