Filter Results By State

A common question I get often is "Why I get no results when searching by state only".
When submitting a search form GEO my WP does a search query between two points based on their coordinates. The search is between the Latitude and longitude of point A which is the address the visitor enters in the address field and point B which are all the locations in the database. GEO my WP search query checks the lat/long of point A against the lat/long of each location in database and pulls the locations which found within the radius entered.
So when one enters only a state ( say Florida ) in the address field GEO my WP does not search for posts within Florida but it will search for posts near the point which represents florida on the map. And if there are no posts near that point no results will show.

This short tutorial will show you a way to overcome this issue and to be able to filter results by state. The tutorial will teach you how to create a simple states select drop-down box which will be added to the search form. When the drop-down changes it will automatically submit the form and will display locations within the chosen state. We will do so by searching the database for locations which have the "state_long" value equal to the value of the drop-down. So this is not a proximity search that we are doing but we will just match the state name from the drop-down menu to the state name of locations in database.

This feature is made out of two part:

  1. States Drop-down Select box - The drop-down menu that will present the choices for the user
  2. GEO my WP Query filter - This is where we modify the search query of GEO my WP

Creating the States Drop-down Menu

Creating the states Drop-down select box is as simple as creating a normal HTML select box. There are two way you we can do that; one way is to manually add the states as options to the select box. This way you can choose which states to include in the select box.
Another way, which is the way I am going to use, is to get all existing states from database and run a loop that will add each state to the select box. This way I can assure that only states that available in the locations table in database will be displayed in the select box. Otherwise, we could have states with no locations.

We will wrap the select-box within a function so it will be easy for us to use it in any of the search forms. All we will have to do is add the function to a search form or we could dynamically call it by using a hook.

Our states drop-down select box is pretty much ready now and we can place it in the functions.php file of our theme/child theme. After done so, In order to display it in our search form we can simply call the function:

anywhere in any of our search forms template file.
we could also use one of the default action hooks which comes with the search forms. For this example I will use the action hook:

to place the select-box just above the radius select-box. To do so we will add the action hook just below our select-box function in the functions.php file:

The select box we just create will automatically submit the form when the state value changes. We do so to prevent conflict with the proximity search that the form provide us with. When a state is being chosen all of the other form values will be ignored ( distance, address and so on ). This is because we do not use a proximity search for that but we simply check for the state value in database. For that same reason we do not do a "selected" check for the select box. When the form submitted using a state the select box value's after page load will be the default (empty) and not the state chosen. Doing it otherwise will make the form search by state even if a use enters and address and want to do a proximity search. This issue will be more clear as we go through the second part of the tutorial below.

Filtering Results

The second part of the code we will create the function that will filter the search query and display locations based on the state chosen in the select box.
to do so we will use the filter:

apply_filters( 'gmw_pt_location_query_clauses', $clauses, $this->form );

which can be found in the file:

geo-my-wp/plugins/posts/includes/gmw-pt-search-functions.php

Using this filter we can modify the search query before it is being executed. The filter is made of "clauses" ( fields, group by,orderby... ) when each clause responsible for a part of the query.
Below is the piece of code that will filter the search query based on a state. You can follow the marks along the lines of code to understand better how it works. And do not forget to place the function below in the function.php file of your theme:

That is it. Give you visitors another way to search for the locations of your site.