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

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:

Read More
$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?

Related posts

1 comment

  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.

Comments are closed.