Support

Account

Home Forums Front-end Issues Populate select list with values of custom field Reply To: Populate select list with values of custom field

  • I’ve been tinkering and finally found something that works:

    
    		<form method="get" action="" onchange="submit();">
    			<div class="input-group">
    				<?php
    				global $wpdb;
    				$meta_key = 'item_oem';
    				$data = $wpdb->get_results($wpdb->prepare( "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key) , ARRAY_N  ); ?>
    				<select class="custom-select" id="oems" aria-label="oems" name="item_oem" >
    					<option selected>Search By OEM</option>
    					<?php foreach( $data as $value ): ?>
    						<option value="<?php echo $value[0]; ?>"><?php echo $value[0]; ?> </option>
    					<?php endforeach; ?>
    			  </select>
    			  <div class="input-group-append">
    			    <a href="/inventory" class="btn btn-secondary" type="button">Clear Filter</a>
    			  </div>
    			</div>
    		</form>
    
    

    I’ll definitely look through your suggestions as well, and see which one works the best.. I don’t want to take a hit on performance just for a filter 🙂

    Thank you!