admin管理员组文章数量:1289957
I want to use two meta_compare
in an array.
// get posts
$posts = get_posts(array(
'post_type' => 'post',
'posts_per_page' => 150,
'meta_query' => array(
array(
'meta_key' => 'usp-custom-1',
'meta_value' => array('question','money', 'health','relationships'),
'meta_compare' => 'IN',
'meta_compare' => '!=',
),
),
'author' => $_GET["id"],
'order' => 'DESC'
));
But it doesn't work.
What I want to achieve is show posts that's NOT equal to these 4 meta_values.
I know I'm probably making a slight mistake somewhere along the lines.
Any help would be appreciated.
I want to use two meta_compare
in an array.
// get posts
$posts = get_posts(array(
'post_type' => 'post',
'posts_per_page' => 150,
'meta_query' => array(
array(
'meta_key' => 'usp-custom-1',
'meta_value' => array('question','money', 'health','relationships'),
'meta_compare' => 'IN',
'meta_compare' => '!=',
),
),
'author' => $_GET["id"],
'order' => 'DESC'
));
But it doesn't work.
What I want to achieve is show posts that's NOT equal to these 4 meta_values.
I know I'm probably making a slight mistake somewhere along the lines.
Any help would be appreciated.
Share Improve this question edited Jul 21, 2021 at 10:55 Buttered_Toast 2,8191 gold badge8 silver badges21 bronze badges asked Jul 21, 2021 at 9:40 robert0robert0 2032 silver badges11 bronze badges 7 | Show 2 more comments2 Answers
Reset to default 1Ok so this way is a bit long but it will get the job done.
We can use multiple meta_value checks with AND relation
// get posts
$posts = get_posts(array(
'post_type' => 'post',
'posts_per_page' => 150,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'usp-custom-1',
'value' => 'question',
'compare' => '!=',
),
array(
'key' => 'usp-custom-1',
'value' => 'money',
'compare' => '!=',
),
array(
'key' => 'usp-custom-1',
'value' => 'health',
'compare' => '!=',
),
array(
'key' => 'usp-custom-1',
'value' => 'relationships',
'compare' => '!=',
),
),
'author' => $_GET["id"],
'order' => 'DESC'
));
You need to check the performance hit for this query because it can be resouce heavy.
I alos noticed $_GET["id"]
, you passed it raw, I would suggest sanitizing/validating every value that was passed to you (values that you did not pass yourself, like user input or url queries).
By the property name I assume that it will be a id, so we can sanitize it like this
'author' => filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT)
Looks like without using meta_
prefix, did the job with NOT IN
as well:
// get posts
$posts = get_posts(array(
'post_type' => 'post',
'posts_per_page' => 150,
'meta_query' => array(
array(
'key' => 'usp-custom-1',
'value' => array( 'question','money', 'health','relationships' ),
'compare' => 'NOT IN',
),
),
'author' => $_GET["id"],
'order' => 'DESC'
));
Thanks to @Buttered_Toast for figuring this out.
本文标签: phpHow to use two metacompare in an array
版权声明:本文标题:php - How to use two meta_compare in an array? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741430011a2378300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'meta_compare' => 'NOT IN',
? – Buttered_Toast Commented Jul 21, 2021 at 9:44usp-custom-1
would make much more sense as a taxonomy, that would mitigate a lot of the performance and scaling issues. – Tom J Nowell ♦ Commented Jul 21, 2021 at 9:47'meta_compare' => 'NOT IN'
still displays posts from all those 4 meta values. – robert0 Commented Jul 21, 2021 at 9:58'meta_compare' => 'NOT IN'
, do you still have twometa_compare
? because there should be only one, the'meta_compare' => 'NOT IN'
– Buttered_Toast Commented Jul 21, 2021 at 10:04