WP_Query 相同的请求,不同的语法 - 其中之一不起作用 [英] WP_Query same request, different syntax - one of which does not work

查看:15
本文介绍了WP_Query 相同的请求,不同的语法 - 其中之一不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行良好的 WP_Query:

I have a WP_Query which works well:

$args = array(
'post_type' => 'product',
'meta_key'  => 'product_subtype',
'meta_value'=> 'public',
'compare'   => '='      
);

但由于我想搜索多个 meta_keys,我尝试了 'array'-语法:

but as I want to search for multiple meta_keys, I tried the 'array'-syntax:

$args = array(
'post_type' => 'product',
'meta_query' => array(                 
                  array(
                    'meta_key'     => 'product_subtype',
                    'meta_value'   => 'public',
                    'compare'      => '='
                      ),
                    ),                  
); 

但它不起作用 - 它给了我所有带有 'post_type' = 'product' 的帖子 - 尽管它是完全相同的请求.我不知道为什么.有人能指出错误吗?

but it does not work - it gives me all the posts with 'post_type' = 'product' - although it is the very same request. I don't know why. Can somebody point out the error?

我按以下方式执行查询(就像我找到的所有教程中所说的那样)

I execute the query in the following way (like it is told in all the tutorials I found)

$the_query = new WP_Query( $args );

就像我说的,第一种方法有效,我只得到带有product_subtype = public"的产品,第二种方法忽略元查询数组.但为什么?

like I said, the first way works and I get only products with "product_subtype = public" the second one ignores the meta query array. But why?

推荐答案

如评论中所述,您错误地使用了 WP_Query.没有大错,但是如果您浏览 Codex for WP_Query,您会注意到 使用没有 meta_ 前缀的 meta_query 数组.

As mentioned in comment, You are using WP_Query wrongly. There is not big mistake but if you go through Codex for WP_Query, You will notice that meta_query array used without meta_ prefix.

因此,如果您从查询中删除 meta_ 前缀,它将按预期工作.

So if you remove meta_ prefix from your query, it will work as expected.

这篇关于WP_Query 相同的请求,不同的语法 - 其中之一不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆