Skip to content

Instantly share code, notes, and snippets.

@Fitoussi
Created April 3, 2015 08:41
Show Gist options
  • Save Fitoussi/ced2430c1e8b29211061 to your computer and use it in GitHub Desktop.
Save Fitoussi/ced2430c1e8b29211061 to your computer and use it in GitHub Desktop.
GMW - Filter Results by State part 5
function gmw_state_filter( $clauses, $gmw ) {
global $wpdb;
/*
* we first do a form check to make sure we only filter the search form that we want.
* for this example I will use form with ID 1
*/
if ( $gmw['ID'] != 1 )
return $clauses;
/*
* check if a state value exists.
* everytime the state value exists in URL the search query below will take over
* and filter results based on that state.
* That is the reason we keep giving the states select box the deafult value on page load.
* To preven visitors from having to manually change it back to deafult when
* they want to do a proximity search based on address instead of state
*/
if ( !isset( $_GET['gmw_state'] ) || empty( $_GET['gmw_state'] ) )
return $clauses;
//get the sate value from URL
$state_value = $_GET['gmw_state'];
//filter the tables clauses
$clauses['fields'] = " wp_posts.*, gmwlocations.* ";
//filter the WHERE cluase to look for locations with based on thier state
$clauses['where'] .= $wpdb->prepare(" AND gmwlocations.state_long = %s", $state_value );
$clauses['groupby'] = "$wpdb->posts.ID";
//order results by state name since we cannot orde rby distance
$clauses['orderby'] = "$wpdb->posts.post_title";
return $clauses;
}
add_filter( 'gmw_pt_location_query_clauses', 'gmw_state_filter', 99, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment