我在主页上有一个搜索表单,它为下面的代码设置相应的url query\\u字符串。当我使用或得到结果时。然而,当我对关系使用“AND”时,我没有得到任何结果,因为用户可以在搜索时选择他们想要选择的选项(查询中有三个选项,如下所示)
有人会建议如何修改查询,以便它必须使用“AND”来搜索准确的结果,但如果用户只使用搜索表单中的rating select下拉列表,那么它仍然会返回这些结果。而不是因为没有使用前两个选项而不显示任何内容。
形式:
<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>
结果:
<?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
);