WP_Query with meta_value LIKE ‘something%’

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

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'

Leave a Comment