I am creating a real estate website on WordPress, I am creating a custom search and am using tax_query to filter search results to display "States" & "Suburbs".
The search works fine, when options are selected. However if nothing is selected, no search results will display at all. This is a problem with a real estate website, because more often than not a user may select a "State" but not the "Suburb", or perhaps nothing at all.
Is there a correct way, or how should I go about doing this so that results will show if nothing (or one or the other) is selected.
Here is my select dropdown for suburbs:
<!-- Suburbs dropdown -->
<select name="suburbs">
<option value="any" selected>Any</option>
<?php
$suburb_terms = get_terms(array(
'taxonomy' => 'suburb',
'hide_empty' => true,
'orderby' => 'name',
) );
foreach( $suburb_terms as $term ) {
echo '<option value="' . $term->slug . '">' . $term->name . '</option>';
}
?>
</select>
And processing the form:
// Suburbs
if( !empty( $_GET['suburbs'] ) ) {
$suburbs = $_GET['suburbs'];
}
// States
if( !empty( $_GET['states'] ) ) {
$states = $_GET['states'];
}
// the query
$property_query = new WP_Query(
array(
'post_type' => 'properties',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'state',
'field' => 'slug',
'terms' => $states,
),
array(
'taxonomy' => 'suburb',
'field' => 'slug',
'terms' => $suburbs,
)
),
)
);
So just to confirm that the search works fine, when dropdowns are selected. But in my case I need results to display if nothing is selected. Anyone know how to achieve this? Thank-you.