0

I have a search form with 3 fields: city porperty type and rooms all fields are select type inputs.

If I select values for each of the fields I get the correct result.

for example:

city= Los Angeles
property type = apartment
room = 3

but if choose only city and property type and dont choose a value for rooms (lets say that the empty value is all). I dont get any result. I would expect to get all apartmnetns in L.A.

this is my wp query I got so far.

if (isset($_GET['front-side-prop-search'])){
    $args = array(
        'posts_per_page' => 9,
        'post_type'     => 'property',
        'meta_query'    =>  array(
            'relation '     => 'AND',
            array(
                'key'       => 'prop_city',
                'value' => $city,
                'compare'       => '=',
                    'meta_query'    =>  array(
                    'relation '  => 'OR',
                        array(
                            'key'       => 'prop_city',
                            'value'     => 'All',
                            'compare'   => 'NOT LIKE',
                        ),
                    ),
                ),
            array(
                'key'       => 'prop_type',
                'value' => $type,
                'compare'       => '='
            ),
            array(
                'key'       => 'prop_rooms',
                'value' => $rooms,
                'compare'       => '='
            ),

        ),
    );
};

I am sure that my wp_query is wrong, please help me or point me to an article that could help me solve this issue.

**

Update - What I finaly did:

**

I checked every variable as tony suggested, than I changed the compare sign on each query depending the variable result.

Knowing that compare => '=' returns all the values that are equal to the value. compare => '!=' will return everything that is not equal to the value.

this is the code i eventually used:

$city = $_GET['field_59ae96d970a06'];  //prop_city
$type = $_GET['field_59ec7533be002'];  //prop_type
$rooms = $_GET['field_59ae9e2370a0b'];


//check for each var if returns empty value or not

if($city == ""){
    $city_val = "!=";
}else{
    $city_val = "=";
};

if($type == "all types"){
    $type_val = "!=";
}else{
    $type_val = "=";
};

if($rooms == "all rooms"){
    $rooms_val = "!=";
}else{
    $rooms_val = "=";
};


//the query:

if (isset($_GET['front-side-prop-search'])){

    $args = array(
        'posts_per_page' => 9,
        'post_type'     => 'property',
        'meta_query'    =>  array(
            'relation '     => 'AND',
            array(
                'key'       => 'prop_city',
                'value' => $city,
                'compare'       => $city_val,
            ),
            array(
                'key'       => 'prop_type',
                'value' => $type,
                'compare'       => $type_val,
            ),
            array(
                'key'       => 'prop_rooms',
                'value' => $rooms,
                'compare'       => $rooms_val,
            ),

        ),
    );
};
4
  • so you say I need to avoid usin an array?
    – DavSev
    Commented Nov 6, 2017 at 19:03
  • (Edited) - just wanted to point out how those logical operators can provide different results. In your case, I'd check the values before creating args. That way, if only $city and $type are selected, your query args only include those 2. codex.wordpress.org/Class_Reference/WP_Query
    – Tony M
    Commented Nov 6, 2017 at 19:25
  • Thank you, if a value is not selected it returns the value 'all' . I think I just dont understand how it works. In my logic I do this: get the value $city, if value is all retreive all data that is not the selected value. Can you please point mo to an example similar to mine?
    – DavSev
    Commented Nov 6, 2017 at 19:31
  • With the meta_query, I don't think value accepts the value all. Codex says: value (string|array). You must supply some string for the value parameter. An empty string or NULL will NOT work. So while supplying "all" will get results, it won't get desired/accurate results. I still think it is easier to check your values before creating the arguments.
    – Tony M
    Commented Nov 6, 2017 at 19:38

1 Answer 1

1

The example below just changes what args is based on your 3 input fields. This eliminates the need to check for these values (and using "all" for value). I'm sure some combination of logical operators for compare and relation would do the job but this works too.

if (isset($_GET['front-side-prop-search'])){

 // test whether $city has a selected value, if so then args below: 

 // $city and $type selected

$args = array(
    'posts_per_page' => 9,
    'post_type'     => 'property',
    'meta_query'    =>  array(
        'relation '     => 'AND',
        array(
            'key'       => 'prop_city',
            'value' => $city,
            'compare'       => '=',
                'meta_query'    =>  array(
                'relation '  => 'OR',
                    array(
                        'key'       => 'prop_city',
                        'value'     => 'All',
                        'compare'   => 'NOT LIKE',
                    ),
                ),
            ),
        array(
            'key'       => 'prop_type',
            'value' => $type,
            'compare'       => '='
        )
    ),
  );


// $city and $rooms

$args = array(
    'posts_per_page' => 9,
    'post_type'     => 'property',
    'meta_query'    =>  array(
        'relation '     => 'AND',
        array(
            'key'       => 'prop_city',
            'value' => $city,
            'compare'       => '=',
                'meta_query'    =>  array(
                'relation '  => 'OR',
                    array(
                        'key'       => 'prop_city',
                        'value'     => 'All',
                        'compare'   => 'NOT LIKE',
                    ),
                ),
            ),
        array(
            'key'       => 'prop_rooms',
            'value' => $rooms,
            'compare'       => '='
        ),

    ),
);

// etc
};
3
  • Thank you , you mean that I need to make an args query for every combination? what should i do if i have 10 or 15 fields?
    – DavSev
    Commented Nov 6, 2017 at 19:46
  • I am trying to understand the query you wrote: select 9 posts from property cpt where meta value is propr_city and value is $city till here i get it. now, how the OR works? like else? let say if the value == All than bring everiything that is not like All?
    – DavSev
    Commented Nov 7, 2017 at 5:48
  • Not for every combination. I had a similar problem a while ago (I only had 3 input fields though). I was using tax_query and all wasn't working. I ended up only checking for all on each field and if it wasn't selected, I could fit the queries with 'relation' = > 'AND'. Definitely not elegant but it got the job done. So you're not checking every option on your select, just when it is all.
    – Tony M
    Commented Nov 7, 2017 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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