I have a search form on my home page that sets up the url query_string accordingly for the code below. When I use or I get results. However when I used "AND" for the relation I dont get any results as the user can select as many of the options as they want to choose from when searching (there are three as seen below in the query)
How would anyone suggest altering the query so that it has to search for the exact results using "AND", but say the user only uses the rating select dropdown in the search form, then it would still return those results. As opposed to not showing anything because the first 2 choices weren't being used.
form:
<form id="resort_search_options" role="search" method="get" class="search-form form-inline" action="/test-results/">
<input type="hidden" value="" name="Price" id="price" disabled>
<input type="hidden" value="" name="Region" id="region" disabled>
<input type="hidden" value="" name="Rating" id="rating" disabled>
<div class="input-group">
<select class="price">
<option value="">Price</option>
<option value="20">$20</option>
<option value="500-1000">$500-1000</option>
<option value="1500–2000">$1500–$2000</option>
<option value="2000–2500">$2000–$2500</option>
<option value="2500–3000">$2500–$3000</option>
<option value="3000–3500">$3000–$3500</option>
<option value="3500–4000">$3500–$4000</option>
<option value="4000–4500">$4000–$4500</option>
<option value="4500–5000">$4500–$5000</option>
<option value="6500–7000">$6500–$7000</option>
<option value="7000-9000">$7000-$9000</option>
<option value="9500–10000">$9500–$10000</option>
<option value="10000-30000">$10000-$30000</option>
<option value="20000">$20000</option>
</select>
<select class="region">
<option value="">Region</option>
<option value="Place1">Place1</option>
<option value="Place2">Place2</option>
<option value="Place3">Place3</option>
<option value="Place4">Place4</option>
<option value="Place5">Place5</option>
</select>
<select class="rating">
<option value="">Rating</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<span class="input-group-btn">
<button type="submit" class="search-submit btn btn-default"><?php _e('Search Resorts', 'roots'); ?></button>
</span>
</div>
</form>
results:
<?php
$queryPrice = explode('-', $_GET['Price']);
$queryRegion = $_GET['Region'];
$queryRating = $_GET['Rating'];
$args = array(
// all your args here
'post_type' => 'resorts',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'price',
'value' => $queryPrice,
'type' => 'NUMERIC',
// 'compare' => 'BETWEEN',
),
array(
'key' => 'region',
'value' => $queryRegion,
// 'compare' => '=',
),
array(
'key' => 'rating',
'value' => $queryRating,
// 'compare' => '=',
),
)
);
$query = new WP_Query( $args );
if($query->have_posts()) : while ($query->have_posts()): $query->the_post();
?>
stuff goes here, yay!
<?php endwhile; else : ?>
<p>No results found, modify your search criteria and try again!</p>
<?php endif; ?>
--
$meta_array = array();
$meta_array = array( 'relation' => 'AND' );
if( isset( $_POST['Price'] ) $meta_array[] = array( //THIS IS LINE 46
'key' => 'price',
'value' => $Price,
'compare' => 'REGEX' );
if( isset( $_POST['Region'] ) $meta_array[] = array(
'key' => 'region',
'value' => $Region,
'compare' => '=' );
if( isset( $_POST['Rating']) $meta_array[] = array(
'key' => 'rating',
'value' => $Rating,
'compare' => 'REGEX' );
$args = array(
'post_type' => 'resorts',
'post_status' => 'publish',
'meta_query' => $meta_array
);