Skip to content

Instantly share code, notes, and snippets.

@Fitoussi
Last active August 29, 2015 14:18
Show Gist options
  • Save Fitoussi/8c0ac31c70518186fef3 to your computer and use it in GitHub Desktop.
Save Fitoussi/8c0ac31c70518186fef3 to your computer and use it in GitHub Desktop.
GMW - Filter Results by State part 4
function gmw_states_dropdown( $gmw ) {
global $wpdb;
//get states exist in locations table in database
//Note that DISTINCT is being used to make sure each state called only once.
$states = $wpdb->get_col("
SELECT DISTINCT `state_long`
FROM {$wpdb->prefix}places_locator
WHERE `state_long` IS NOT NULL
and trim(coalesce(state_long, '')) <>''
ORDER BY `state_long`");
?>
<!--- create the states select box.-->
<!--- Note that the javascript that automatically submits the form when state value changes -->
<select class="gmw-state-dropdown" name="gmw_state"
onchange="jQuery(this).closest('form').submit()">
<option value="" selected="selected">State</option>
<!--- loop through states and add to the select box -->
<?php foreach ( $states as $state ) { ?>
<option value="<?php echo $state; ?>"><?php echo $state; ?></option>
<?php } ?>
</select>
<?php
}
add_action( 'gmw_search_form_before_distance', 'gmw_states_dropdown', 15 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment