Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I noticed that there are bunch of operator can be use for compare in meta_query. However, I am not quite sure what operator I should use, it is somehow confusing like = and LIKE operator.

I would like to know what exactly each operator mean, and in what condition I should use them.

=
!=
>
>=
<
<=
LIKE
NOT LIKE
IN
NOT IN
BETWEEN
NOT BETWEEN
NOT EXISTS

Thanks.

share|improve this question
add comment

2 Answers

up vote 12 down vote accepted

The first several work about like you would expect:

=   equals
!=  does not equal
>   greater than
>=  greater than or equal to
<   less than
<=  less than or equal to

LIKE and NOT LIKE are SQL operators that let you add in wild-card symbols, so you could have a meta query that looks like this:

array( 
    'meta_key' => 'name', 
    'meta_value' => 'Pat', 
    'meta_compare' => 'LIKE'
)

This would return all posts where the meta value "name" has the string "Pat". In this case, "Pat" "Patricia" and "Patrick" would all be returned back to you. There's a non-WordPress tutorial explanation here.

Adding the wildcard character % isn't necessary, because it gets added by default like @Herb said in his below answer. Like this: $meta_value = '%' . like_escape( $meta_value ) . '%'; - see source.

BETWEEN and NOT BETWEEN allow you to define a range of values that could be correct, and require you to give two values in an array in your meta_query:

array( 
    'meta_key' => 'price', 
    'meta_value' => array(20,30) 
    'meta_compare' => 'BETWEEN'
)

This will get you all posts where the price is between 20 and 30. This person digs into an example with dates.

NOT EXISTS is just like what it sounds - the meta value isn't set or is set to a null value. All you need for that query is the key and comparison operator:

array( 
    'meta_key' => 'price', 
    'meta_compare' => 'NOT EXISTS'
)

This person needed to query non-existant meta values, and needed them to play nice with others.

Hope this helps!

share|improve this answer
1  
Beat me to it. So, +1 for the explanation! –  userabuser Oct 29 '12 at 18:50
1  
lol userabuser, you were editing the original question! thanks for the upvote! –  guiniveretoo Oct 29 '12 at 18:54
    
Thanks a lot!! This is exactly what I needed! –  dev-jim Oct 29 '12 at 18:55
add comment

Note that when using a meta_compare value of 'LIKE', WordPress automatically wraps the wildcard character ( % ) around the meta_value string. So the 'Pat%' example could fail to return any results.

share|improve this answer
    
is there info about that in the docs somewhere Herb? Should the example change to remove the %? –  guiniveretoo Nov 22 '13 at 15:47
    
It should, I actually did that right now, see the source, then it gets very clear that Herb is right. @guiniveretoo –  ialocin Apr 8 at 15:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.