CodeCanyon

Wordpress Multiple meta_query

202 posts
  • France
  • Most Wanted Bounty Winner
  • Has been a member for 4-5 years
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Microlancer Beta Tester
  • Referred between 10 and 49 users
  • Bought between 100 and 499 items
corsonr says

Hi,

i’m working a wordpress query based on meta_query and i have a little problem, here is the query:

$query = new WP_Query( array ( 'post_type' => $post_type, 'posts_per_page' => $posts_per_page, 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'key_one', 'value' => 'value_one', 'compare' => 'LIKE' ), array( 'key' => 'key_one', 'value' => 'value_two', 'compare' => 'LIKE' ), array( 'key' => 'key_two', 'value' => 'value_key_two', 'compare' => 'NOT LIKE' ) ), 'orderby' => $orderby, 'order' => $order ) );

That means: get posts where key_one LIKE value_one OR key_one LIKE value_two AND key_two NOT LIKE value_key_two…

and the thing is that it seems that it’s working i have only have the OR condition, but when i add the latest part (AND key_two NOT LIKE value_key_two) the query does not match…

so my question is: is it possible to have complex queries like that in meta_query? If yes, how ?

Thanks!

202 posts
  • France
  • Most Wanted Bounty Winner
  • Has been a member for 4-5 years
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Microlancer Beta Tester
  • Referred between 10 and 49 users
  • Bought between 100 and 499 items
corsonr says

Auto-answer… the tweak is to use ‘IN’ in compare argument for key_one…

like that:

$query = new WP_Query( array ( 'post_type' => $post_type, 'posts_per_page' => $posts_per_page, 'meta_query' => array( array( 'key' => 'key_one', 'value' => array( 'value_one', 'value_two' ), 'compare' => 'IN' ), array( 'key' => 'key_two', 'value' => 'value_key_two', 'compare' => 'LIKE' ) ), 'orderby' => $orderby, 'order' => $order ) );
by
by
by
by
by