查询多个元键值?
4 个回答
- 投票数
-
- 2012-03-13
我觉得这里有一个AND/OR混乱.
OP中的查询将仅返回具有两者 key1='value1'和key2='value2'的帖子.大多数WP插件(无论如何,我都知道)不会在postmeta中使用相同的密钥在同一帖子中存储多个值.
如果您真正想要的是OR(您想获得key1='value1'的帖子,以及key1='value2'的帖子),请使用@IN和一个@WhiskerSandwich的答案值参数的值数组.
或者,您可以为"meta_query"提供
relation
参数:$args = array( 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'key1', 'value' => 'value1', 'compare' => '=' ), array( 'key' => 'key1', 'value' => 'value2', 'compare' => '=' ) ) );
请注意,将OR用作使用同一键的多个元查询的关系,等同于使用
IN
和单个值的数组.I feel like there's an AND/OR confusion going on here.
The queries in the OP will only return posts which have both key1 = 'value1' AND key2 = 'value2'. Most WP plugins (that I know of, anyway) do not store multiple values in postmeta, for the same post, using the same key.
If what you want is really an OR (you want to get the posts where key1 = 'value1', as well as the posts where key1 = 'value2'), then see @WhiskerSandwich's answer, using 'IN' and an array of values for the value parameter.
Alternatively, you can provide a
relation
parameter to `meta_query':$args = array( 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'key1', 'value' => 'value1', 'compare' => '=' ), array( 'key' => 'key1', 'value' => 'value2', 'compare' => '=' ) ) );
Note that using OR as the relation for multiple meta queries using the same key is the functional equivalent of using
IN
and an array of values for a single one.-
谢谢你,布恩我不知道存在"关系"参数.帮助我.Thanks for this, Boone. I didn't know the "relation" param existed. Helped me out.
- 0
- 2012-03-21
- MathSmath
-
如果您只需要搜索一个键,则此方法有效.如果您有两个或多个,则可能需要在关系参数中使用" AND"将其组合,在这种情况下,@ WhiskerSandwich的以下答案更合适.This works if you only have one key to search on. If you have two or more, you may need to use 'AND' to combine them in the relationship parameter, in which case @WhiskerSandwich's answer below is the better fit.
- 0
- 2015-04-21
- SinisterBeard
-
- 2012-03-13
我遇到了同样的问题,即无法为同一键传递多个数组.相反,只需使用一个数组,将"值"设置为值数组,然后将"比较"设置为IN:
<?php $args = array( 'meta_query' => array( array( 'key' => 'key1', 'value' => array('value1', 'value2'), 'compare' => 'IN' ), ) ); $query = new WP_Query( $args ); ?>
I had the same problem where passing multiple arrays for the same key wasn't working. Instead, just use one array, set 'value' to an array of values, and set 'compare' to IN:
<?php $args = array( 'meta_query' => array( array( 'key' => 'key1', 'value' => array('value1', 'value2'), 'compare' => 'IN' ), ) ); $query = new WP_Query( $args ); ?>
-
- 2012-01-27
您必须将postmeta表作为第二个值的别名:
$querystr = " SELECT $wpdb->posts.* FROM $wpdb->posts, $wpdb->postmeta, $wpdb->postmeta AS mt1 WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = 'key1' AND $wpdb->postmeta.meta_value = 'value1' AND mt1.meta_key = 'key1' AND mt1.meta_value = 'value2' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ORDER BY $wpdb->posts.post_date DESC ";
从3.1开始,您还可以使用
meta_query
a>:$args = array( 'meta_query' => array( array( 'key' => 'key1', 'value' => 'value1', 'compare' => '=' ), array( 'key' => 'key1', 'value' => 'value2', 'compare' => '=' ) ) ); $query = new WP_Query( $args );
You have to alias the postmeta table for the second value:
$querystr = " SELECT $wpdb->posts.* FROM $wpdb->posts, $wpdb->postmeta, $wpdb->postmeta AS mt1 WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = 'key1' AND $wpdb->postmeta.meta_value = 'value1' AND mt1.meta_key = 'key1' AND mt1.meta_value = 'value2' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ORDER BY $wpdb->posts.post_date DESC ";
You can also do this now since 3.1 with a
meta_query
:$args = array( 'meta_query' => array( array( 'key' => 'key1', 'value' => 'value1', 'compare' => '=' ), array( 'key' => 'key1', 'value' => 'value2', 'compare' => '=' ) ) ); $query = new WP_Query( $args );
-
您好Milo感谢您的回答.SQL不返回任何值.除非我从数组中删除第二个键和值,否则数组也不会返回任何值.那么,这有问题吗?Hi Milo thanks for answering. The SQL returns no value. And the array is returning no value as well unless I remove the 2nd key and value from the array. So is this bugged?
- 0
- 2012-01-27
- steen
-
@steen-我不确定您的问题是什么,我已经测试了这两种方法,并且它们在我的3.3.1安装中都有效.您的密钥字面意思是" key1",值是" value1"和" value2"吗?如果在查询后立即执行print_r($the_query);,您什么都看不到?@steen - I'm not sure what your issue is, I've tested both methods and they are working in my install of 3.3.1. Is your key literally 'key1' and values 'value1' and 'value2'? Do you see nothing if you `print_r( $the_query );` immediately after the query?
- 0
- 2012-01-27
- Milo
-
- 2012-01-27
键是key1,值'value1'和'value2'在21的全新安装中同时尝试了文本和数字.print_r($the_query);作品输出看起来正常.还尝试过key1和key2也不起作用.只要将其限制为一个数组,它就会起作用.已使用其他浏览器检查.
这确实起作用.
<?php $args = array( 'meta_query' => array( array( 'key' => 'wtf', 'value' => '1', 'compare' => '>=' ), // this array results in no return for both arrays array( 'key' => 'wtf', 'value' => '2', 'compare' => '<=' ) ) ); $the_query = new WP_Query( $args ); ?>
Key is key1 and values 'value1' and 'value2' tried it both text and numeric in a fresh install with twenty eleven. print_r( $the_query ); works output looks normal. Also tried key1 and key2 also doesn't work. It works as soon as I limit it to to one array. Checked with different browsers.
This however does work.
<?php $args = array( 'meta_query' => array( array( 'key' => 'wtf', 'value' => '1', 'compare' => '>=' ), // this array results in no return for both arrays array( 'key' => 'wtf', 'value' => '2', 'compare' => '<=' ) ) ); $the_query = new WP_Query( $args ); ?>
如何查询具有相同键的多个元键值
下一个代码