Puddinq.com sharing knowledge
';

WordPress: Set default term for custom taxonomy

WordPress: Set default term for custom taxonomy

function set_default_term_for_custom_tax( $post_id, $post ) {
    if ( 'publish' === $post->post_status ) {
        $defaults = array(
            'department' => array( 'office', 'info' ),
            'subject' => array( 'news' ),
            );
        $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', 'set_default_term_for_custom_tax', 10, 2 );