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?
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 thatmeta_query
array used withoutmeta_
prefix.So if you remove
meta_
prefix from your query, it will work as expected.