元查询比较运算符说明
2 个回答
- 投票数
-
- 2012-10-29
您期望的前几项工作:
= equals != does not equal > greater than >= greater than or equal to < less than <= less than or equal to
喜欢和不喜欢
LIKE
和NOT LIKE
是SQL运算符,可用于添加通配符,因此您可以使用如下所示的元查询:array( 'key' => 'name', 'value' => 'Pat', 'compare' => 'LIKE' )
这将返回元值"name"具有字符串" Pat"的所有帖子.在这种情况下," Pat"," Patricia"和" Patrick"将全部退还给您.在此处.
不需要添加通配符
%
,因为默认情况下会添加通配符,如@Herb在其下面的答案.像这样:$meta_value = '%' . like_escape( $meta_value ) . '%';
-请参见来源.
IN 和 NOT IN
IN
和NOT IN
选择在给定数组中(或不在给定数组中)的任何匹配项.因此,您可以执行以下操作:array( 'key' => 'color', 'value' => array('red', 'green', 'blue') 'compare' => 'IN' )
,它将获得所有颜色设置为红色,绿色或蓝色的帖子.使用" NOT IN"得到相反的结果,即任何将值设置为数组中值以外的值的帖子.
为此生成的SQL如下所示:
SELECT * FROM posts_meta WHERE value IN ("red", "green", "blue")
更好和不喜欢
BETWEEN
和NOT BETWEEN
允许您定义可能正确的值范围,并要求您在meta_query的数组中提供两个值:>array( 'key' => 'price', 'value' => array(20,30) 'compare' => 'BETWEEN' )
这将为您提供价格在20到30之间的所有帖子.这个人深入研究一个带有日期的示例.
不存在
NOT EXISTS
听起来很像-未设置元值或将其设置为空值.该查询所需的全部就是键和比较运算符:array( 'key' => 'price', 'compare' => 'NOT EXISTS' )
此人需要查询非存在的元值,并需要它们与他人一起玩耍.
希望这会有所帮助!
The first several work as you would expect:
= equals != does not equal > greater than >= greater than or equal to < less than <= less than or equal to
LIKE and NOT LIKE
LIKE
andNOT LIKE
are SQL operators that let you add in wild-card symbols, so you could have a meta query that looks like this:array( 'key' => 'name', 'value' => 'Pat', 'compare' => 'LIKE' )
This would return all posts where the meta value "name" has the string "Pat". In this case, "Pat" "Patricia" and "Patrick" would all be returned back to you. There's a non-WordPress tutorial explanation here.
Adding the wildcard character
%
isn't necessary, because it gets added by default like @Herb said in his below answer. Like this:$meta_value = '%' . like_escape( $meta_value ) . '%';
- see source.
IN and NOT IN
IN
andNOT IN
select any matches that are in (or not in) the given array. So you could do something like this:array( 'key' => 'color', 'value' => array('red', 'green', 'blue') 'compare' => 'IN' )
and it would get all posts that have the color set to either red, green, or blue. Using 'NOT IN' gets the reverse, any posts that have a value set to anything else than what's in the array.
The generated SQL for this would look something like this:
SELECT * FROM posts_meta WHERE value IN ("red", "green", "blue")
BETWEEN and NOT BETWEEN
BETWEEN
andNOT BETWEEN
allow you to define a range of values that could be correct, and require you to give two values in an array in your meta_query:array( 'key' => 'price', 'value' => array(20,30) 'compare' => 'BETWEEN' )
This will get you all posts where the price is between 20 and 30. This person digs into an example with dates.
NOT EXISTS
NOT EXISTS
is just like what it sounds - the meta value isn't set or is set to a null value. All you need for that query is the key and comparison operator:array( 'key' => 'price', 'compare' => 'NOT EXISTS' )
This person needed to query non-existent meta values, and needed them to play nice with others.
Hope this helps!
-
注意:如果您使用的是`meta_query`数组,则您的键不应以`meta_`为前缀.如果您使用的是$ query->meta_key,$ query->meta_value等,则它们仍应保留前缀.Note: If you're using `meta_query` array, your keys should not be prefixed with `meta_`. If you're using `$query->meta_key`, `$query->meta_value`, etc. then these should still retain the prefix.
- 1
- 2014-08-20
- Sean
-
我似乎找不到" IN"比较选项的解释.任何想法如何工作?I can't seem to find an explanation on what the "IN" compare option does. Any idea how that works?
- 0
- 2015-03-09
- Joe
-
@Joe,我不知道为什么我没有添加有关" IN"和" NOT IN"的任何内容.我已经用这些比较来编辑和更新了答案.@Joe, I don't know why I didn't add anything about "IN" and "NOT IN". I've edited and updated the answer with those comparisons.
- 2
- 2015-03-09
- Jen
-
这是一个很好的答案,但是现在有更多可用的选项:: https://codex.wordpress.org/Class_Reference/WP_Meta_QueryThis is a great answer but there are some more options available now-: https://codex.wordpress.org/Class_Reference/WP_Meta_Query
- 0
- 2020-08-28
- noelmcg
-
'EXISTS','REGEXP','NOT REGEXP'和'RLIKE''EXISTS' , 'REGEXP', 'NOT REGEXP' and 'RLIKE'
- 0
- 2020-08-28
- noelmcg
-
- 2013-10-13
请注意,当使用meta_compare值'LIKE'时,WordPress会自动在meta_value字符串周围包裹通配符(%).因此," Pat%"示例可能无法返回任何结果.
Note that when using a meta_compare value of 'LIKE', WordPress automatically wraps the wildcard character ( % ) around the meta_value string. So the 'Pat%' example could fail to return any results.
-
在Herb的文档中是否有关于此的信息?该示例是否应更改为删除"%"?is there info about that in the docs somewhere Herb? Should the example change to remove the `%`?
- 0
- 2013-11-22
- Jen
-
应该,实际上我现在已经这样做了,请参见[source](https://core.trac.wordpress.org/browser/tags/3.8.1/src/wp-includes/meta.php#L841),然后很清楚赫伯是对的.@guiniveretooIt should, I actually did that right now, see the [source](https://core.trac.wordpress.org/browser/tags/3.8.1/src/wp-includes/meta.php#L841), then it gets very clear that Herb is right. @guiniveretoo
- 0
- 2014-04-08
- Nicolai
我注意到meta_query中可以使用一堆运算符进行比较. 但是,我不太确定应该使用什么运算符,它有点像
=
和LIKE
运算符那样令人困惑.我想知道每个运算符的确切含义,以及在什么情况下应该使用它们.
谢谢.