Creating a Dropdown Custom Meta Field in WordPress

Here’s a helpful snippet. Let’s say you want to add a State dropdown as a custom meta field on a post, page or custom post type. It can be kind of a pain but if you add the names and values as key value pairs you can loop through the array and output the HTML pretty easily.

First get the value of _cpt_state (if it has one) and assign it to $state

if ( ! $state = get_post_meta( $post->ID, '_cpt_state', true ) ) $state = '';