I have a custom post type "vegetables"
I have a series of custom fields for each month of the year as check boxes
I have a custom taxonomy "Seasons" that I want to assign based on what boxes are checked in my custom fields
Summer if any June July August
Fall if any September October November
Winter if any December January February
Spring if any March April May
So the vegitable could be assigned all season or a single season based on what custom fields are checked
add_action( 'save_post', 'assign_cat_to', 10, 1 );
function assign_cat_to( $post_id ) {
if ( 'seasonal-product' !== get_post_type( $post_id ) ) {
return;
}
$term_slugs = array();
foreach ( $_POST as $field => $value ) {
if ( 0 === strpos( $field, 'wpcf-available-in-' ) && '1' === $value ) {
$term_slugs[] = str_replace( 'wpcf-available-in-', '', $field );
}
}
if ( empty( $term_slugs ) ) {
return;
}
$term_ids = array();
foreach ( $term_slugs as $term_slug ) {
$term = get_term_by( 'slug', $term_slug, 'season' );
$term_ids[] = $term->term_id;
}
wp_set_object_terms( $post_id, $term_ids, 'season' );
}
I have made some updates based on actual fields and terms but I still do not understand how to assign "spring, summer, fall, winter" to the term.
Alternatively I could store the season name "spring, summer, fall, winter" to the database instead of "1" would that make it easier to write to the term slug?