Meta query with string starting like pattern

You could try the REGEXP version:

    'meta_query' => array(
        array(
            'key'       => 'email_address',
            'value'     => '^hello@',
            'compare'   => 'REGEXP',
        )
    )

where ^ matches the beginning of a string.

You can check the MYSQL reference on REGEXP here for more info.

Notice that these are the possible values of the meta compare parameter:

'=', '!=', '>', '>=', '<', '<=', 
'LIKE', 'NOT LIKE','IN', 'NOT IN', 
'BETWEEN', 'NOT BETWEEN', 'NOT EXISTS', 
'REGEXP', 'NOT REGEXP', 'RLIKE'

according to the WordPress 3.9.2 source.

From the MYSQL ref:

Name        Description
------------------------------------------------------
NOT REGEXP  Negation of REGEXP
REGEXP      Pattern matching using regular expressions
RLIKE       Synonym for REGEXP

Leave a Comment