gpt4 book ai didi

php - WordPress wpdb 中的 AND 和 OR 查询

转载 作者:行者123 更新时间:2023-11-30 00:44:48 25 4
gpt4 key购买 nike

对了,我有read through the entire wpdbezSQL所以我对 wpdb 类的功能有一个很好的理解...本质上 wpdb 之于 SQL 就像 jQuery 之于 Javascript!

由于我以前从未这样做过,所以我不会因为没有尝试 SQL 而感到太难过,但这就是我今天所做的,以表明我已经尝试过了,只是后来才发现 AND 和 OR 不能使用在 WP_Query 中:\

最终结果应该是,如果用户从“产品类型”下拉列表中进行选择并单击搜索按钮,页面应根据选择菜单中的该术语返回结果。如果他们从 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>

我希望这是有道理的,所以如果有人能给我一个起点,我将不胜感激。

最佳答案

您可以使用这样的关系条件:--

$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
'relation'=>'AND', // or OR
array(
'key' => 'product_type',
'value' => $ProductType,
'compare' => 'LIKE'
),
array(
'key' => 'product_group',
'value' => $ProductGroup,
'compare' => 'LIKE'
)
)
);

您还可以根据需要更改关系...

关于php - WordPress wpdb 中的 AND 和 OR 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21467874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com