3

I have read several questions like this (for example, this), but no equal.

I WP_Query need to perform a LIKE on a meta_value. My problem is that I want the like is as follows: meta_value LIKE 'value%' and I can not solve.

My code is as follows:

$today = '0702'; /*(month and day)*/

$args = array (
'post_type' => 'post',
'posts_per_page' => $number,
'orderby' => $sort_by,
'cat' => '2'
'post_status' => 'publish'
'ignore_sticky_posts' => true,
'meta_key' => 'date'
'meta_value' => $today,
'meta_compare' => 'LIKE'
);

Thanks!!! :)

1 Answer 1

9

For a left-sided match you can get around the automatic adding of '%''s by WP by using regular expression RLIKE:

'meta_value' => '^' . preg_quote( $today ),
'meta_compare' => 'RLIKE'

Similarly for right-sided match:

'meta_value' => preg_quote( $today ) . '$',
'meta_compare' => 'RLIKE'
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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