How to set a Preset category for custom post types?

Try hooking into the save_post action to check the custom terms when a post gets saved:

add_action( 'save_post', 'set_default_category' );
function set_default_category( $post_id ) {

    // Get the terms
    $terms = wp_get_post_terms( $post_id, 'your_custom_taxonomy');

    // Only set default if no terms are set yet
    if (!$terms) {
        // Assign the default category
        $default_term = get_term_by('slug', 'your_term_slug', 'your_custom_taxonomy');
        $taxonomy = 'your_custom_taxonomy';
        wp_set_post_terms( $post_id, $default_term, $taxonomy );
    }
}

Leave a Comment