I am using WP_Query to sorting the post by user input. The input contains meta value which is display in select type field.
Normally when there is value inserted into the form, the wp_query working fine. But how about when the user want to sorting all the posts regardless what the meta is value is?
Here is what I mean:
//this is my input form
<select name"rating">
<option value="">Show all</option> //if user select this, show all post, regardless the rating
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
//here is for WP_Query
$args = array(
'post_type'=>array('listing','renting'),
'posts_per_page'=> 6,
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'rating',
'value' => $_GET['rating']//here is what I don't understand, what if no value in this field?is it take the value as "0"?
),
......
),
As you can see, in the select form, there is a option that will show all the posts regardless the rating. How do I suppose to do so?
thanks
check if the $GET[‘rating’] is set ( isset($_GET[‘rating’]) or has any value. ( !empty($_GET[‘rating’])
if not, build the query fecthing all post without ‘meta_query’
for example:
warning: i didn’t tested the new code, so the the
meta_query
line might break the query if $meta is empty.