[wp-hackers] meta_value contains text

Ashok Padda ashok.padda at gmail.com
Sun Aug 16 18:37:49 UTC 2009

Hi Jorn,

query_posts() prepares the where condition and executes the sql. So we have
to use it prepare a condition with a wild card, like 'and meta_value like
"New%"' in your case. meta_value will accept 'New%' as a value, but the
compare operator is always '='. To force it to use another operator we have
to use meta_compare. So, you can rewrite it to following.


However, meta_compare allows only following additional operators (!=, >, >=,
<, <=) and does not allow like.

You can make changes to line no. 2146 in in wp-includes/query.php.
--            if ( ! isset($q['meta_compare']) || empty($q['meta_compare'])
|| ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )
++            if ( ! isset($q['meta_compare']) || empty($q['meta_compare'])
|| ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<',
'<=','like')) )

This does work in your case. But, wordpress team may have had a reason in
not allowing wild cards while searching meta_value. So, please use it if
only required.


On Sun, Aug 16, 2009 at 10:45 PM, Jörn Röder <kontakt at joernroeder.de> wrote:

> Hi,
> i have some trouble with the query_posts() and meta_value-Parameter
> if i try query_posts('meta_value=New York') it works, but with
> 'meta_value=New' i get no results even though it was included in 'New York'…
> Is there a "meta_compare-Value" that is not mathematical and contain works?
> Thanks Jörn
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list