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.

1 Answer
1

So far good, add taxonomy condition by checking the suburbs and states Value.

// Suburbs
if( !empty( $_GET['suburbs'] ) ) {
    $suburbs = $_GET['suburbs'];
}

// States
if( !empty( $_GET['states'] ) ) {
    $states = $_GET['states'];
}

// Query arguments.
$args = array(
            'post_type'      => 'properties',
            'posts_per_page' => 10,
        );

$taxquery = array();

// if $state variable is selected.
if(!empty($states) || isset($suburbs)  ){
    array_push($taxquery,array(
            'taxonomy' => 'state',
            'field' => 'slug',
            'terms' => $states,
        ));
}

// if $suburbs variable is selected.
if(!empty($suburbs) || isset($suburbs) ) ){
    array_push($taxquery,array(
            'taxonomy' => 'suburb',
            'field' => 'slug',
            'terms' => $suburbs,
        ));
}

// if $taxquery has array;
if(!empty($taxquery)){
    $args['tax_query'] = $taxquery;
}

// And finally fetch the all post.
$property_query = new WP_Query($args);

Replace your PHP code with this. It will definitely solve your problem 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *