Save selected item from dropdown menu in a meta box as a metadata value for a custom post type

$myterms = get_terms($taxonomies, $args); returns an array of term objects. Use $term->term_id as option value …

"<option value="$term->term_id">". esc_html( $term_name ) . "</option>"

… and store that ID, not the name. Names can change, the IDs will stay the same – unless someone deletes a term and creates a new one with the same name.

To select the correct option use selected():

"<option value="$term->term_id"" . selected( $term->term_id, $MC_Catalog_course_area, FALSE ) .">"

You can validate the value with absint() then.

Your save_post handler needs more checks.

Leave a Comment