WP_Query same request, different syntax - one of which does not work

本小妞迷上赌 提交于 2019-12-23 12:37:05

问题


I have a WP_Query which works well:

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

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'      => '='
                      ),
                    ),                  
); 

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 );

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?


回答1:


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.

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



来源:https://stackoverflow.com/questions/31182918/wp-query-same-request-different-syntax-one-of-which-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!