2

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.

3

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 :)

5
  • Thanks very much Govind! That worked - you genius. Do you know how I can pass 'relation' => 'OR' into that? As when only suburbs are selected they do not display?
    – Notorious
    Nov 25 '16 at 14:23
  • 1
    I just tried too but my rep is too low :( I'll get my rep up and will make sure to come back here and vote it up :)
    – Notorious
    Nov 25 '16 at 14:25
  • for that i will modify my answer asap. ;)
    – GKS
    Nov 25 '16 at 14:29
  • Don't worry I got it (dumb question), I added it into the $taxquery array. Cheers mate :)
    – Notorious
    Nov 25 '16 at 14:32
  • thats great ! :)
    – GKS
    Nov 25 '16 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.