Home Forums General Issues Get options from custom field as select?


Get options from custom field as select?

  • How do I get all options from a custom fields select, and render a drop down on the frontend from it with the exact same values as is displayed in the backend?

  • For the sake of needing this at a later point:

    	$field_key = "field_5925c714d03e7"; // <-- Find this by turning on Field Keys under Screen Options in admin interface
    	$field = get_field_object($field_key);
    	if( $field ) {
    		echo '<select id="brands" name="brands" class="brands" autocomplete="off">';
    		echo '<option value="-1">All</option>';
    		foreach( $field['choices'] as $k => $v ) {
    			echo '<option value="' . $k . '"';
    			if ( $k == $brand ) {
    				echo ' selected';
    			echo '>' . $v . '</option>';
    		echo '</select>';
  • This demonstrates a good use of get_field_object(). Until I found this I was maintaining a separate lookup table in PHP because I couldn’t figure out a way of reading select labels from values on front-end pages without a result set.

    Kudos to @blaasvaer for sharing this.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Get options from custom field as select?’ is closed to new replies.