admin管理员组文章数量:1298449
I have the following WP Query:
if(isset($_GET['scope']) && !empty($_GET['scope'])) {
$scope = $_GET['scope'];
} else {
$scope = '';
}
$foo = new WP_Query([
'post_type' => 'people',
'posts_per_page' => -1,
'meta_query' => [
[
'key' => 'name',
'value' => $scope,
'compare' => '='
]
]
]);
And this dropdown menu:
<form class="criteria-form" action="<?=site_url('/page..')?>">
<select name="scope" class="criteria-select" onchange="this.form.submit()">
<option value="" <?= $scope ? 'selected' : '' ?>>All Foo</option>
<option value="foo-red" <?= $scope == 'Foo Red' ? 'selected' : '' ?>>Foo Red</option>
<option value="Foo-green" <?= $scope == 'Foo Green' ? 'selected' : '' ?>>Foo Green</option>
</select>
</form>
All is working good for options Foo Red
and Foo green
because it returns specified values, but how can I select all values using the same compare operator? What I want to do is when user selects All foo
he gets Red and Green. If I use LIKE
operator instead of =
I get Red and Green Foos mixed.
Maybe there is some better way to change operator for that specific select option and get all results.
I have the following WP Query:
if(isset($_GET['scope']) && !empty($_GET['scope'])) {
$scope = $_GET['scope'];
} else {
$scope = '';
}
$foo = new WP_Query([
'post_type' => 'people',
'posts_per_page' => -1,
'meta_query' => [
[
'key' => 'name',
'value' => $scope,
'compare' => '='
]
]
]);
And this dropdown menu:
<form class="criteria-form" action="<?=site_url('/page..')?>">
<select name="scope" class="criteria-select" onchange="this.form.submit()">
<option value="" <?= $scope ? 'selected' : '' ?>>All Foo</option>
<option value="foo-red" <?= $scope == 'Foo Red' ? 'selected' : '' ?>>Foo Red</option>
<option value="Foo-green" <?= $scope == 'Foo Green' ? 'selected' : '' ?>>Foo Green</option>
</select>
</form>
All is working good for options Foo Red
and Foo green
because it returns specified values, but how can I select all values using the same compare operator? What I want to do is when user selects All foo
he gets Red and Green. If I use LIKE
operator instead of =
I get Red and Green Foos mixed.
Maybe there is some better way to change operator for that specific select option and get all results.
Share Improve this question asked Aug 12, 2022 at 8:08 MaaverickMaaverick 51 bronze badge1 Answer
Reset to default 0You can put your meta_query
in a variable to achieve the desired result. Then you can fill the variable with your actual meta_query when your condition is met. Check out the code below.
$meta_query = array();
if ( isset($_GET['scope']) && !empty($_GET['scope']) ) {
$meta_query[] = array(
'key' => 'name',
'value' => $_GET['scope'],
'compare' => '='
);
}
$foo = new WP_Query([
'post_type' => 'people',
'posts_per_page' => -1,
'meta_query' => array(
$meta_query
)
]);
本文标签: phpWPQuery How to get results from both metakey options
版权声明:本文标题:php - WP_Query: How to get results from both meta_key options? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738466052a2088311.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论