【发布时间】:2014-02-23 10:16:05
【问题描述】:
对,我有 read through the entire wpdb 和 ezSQL,所以我非常了解 wpdb 类可以做什么......本质上 wpdb 之于 SQL 就像 jQuery 之于 Javascript!
因为我以前从未这样做过,所以我不会因为不尝试 SQL 而感到太难过,但这就是我今天所做的,以表明我已经尝试过,只是后来才读到 AND 和 OR 不能使用在 WP_Query 中:\
最终结果应该是,如果用户从 ProductType 下拉菜单中选择 并单击搜索按钮,则页面应根据选择菜单中的该术语返回结果。如果他们从 ProductGroup 中选择一个选项并点击搜索按钮,则需要根据选择返回结果。
如果两个下拉菜单都选择了选项,则 ProductType 和 ProductGroup 都需要查询并返回结果。
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'relation'=>'AND', // or OR
'relation'=>'OR', // this is naturally not correct
'meta_query' => array(
array(
'key' => 'product_type',
'value' => $ProductType,
'compare' => 'LIKE'
),
array(
'key' => 'product_group',
'value' => $ProductGroup,
'compare' => 'LIKE'
)
)
);
<form name="x" action="" method="GET" class="search-results-form" >
<select name="productType" class="search-product-type">
<option value="">Product Type</option>
<option value="ProductType1">ProductType1</option>
<option value="ProductType2">ProductType2</option>
</select>
<select name="productGroup" class="search-product-type">
<option value="">Product Type</option>
<option value="ProductGroup1">ProductGroup1</option>
<option value="ProductGroup2">ProductGroup2</option>
</select>
<input type="submit" value="SEARCH" class="submit-button btn" />
</form>
我希望这是有道理的,所以如果有人能给我一个起点,我将不胜感激。
【问题讨论】: