ControlledChaos
7/13/2017 - 2:32 PM

Make Default taxonomy term for Custom Post Type - WordPress

Make Default taxonomy term for Custom Post Type - WordPress

<?php

function ccd_set_default_object_terms( $post_id, $post ) {
    if ( 'publish' === $post->post_status ) {
        $defaults = array(
            'post_tag' => array( 'jdm', 'bre' ),
            'cars'     => array( 'datsun' ),
            );
        $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', 'ccd_set_default_object_terms', 100, 2 );

?>

Default Taxonomy Term

WordPress Snippet