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,
),
),
);
};
$city
and$type
are selected, your queryargs
only include those 2. codex.wordpress.org/Class_Reference/WP_Querymeta_query
, I don't thinkvalue
accepts the valueall
. 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.