get_posts with meta_compare=’LIKE’ not working

meta_compare Possible values are '!=', '>', '>=', '<', or '<='. Default value is '='

if you want to use LIKE you need to create a meta_query eg:

$tolettpe = "Sale";//default
if($_REQUEST['tolettype']) $tolettpe = $_REQUEST['tolettype'];
else if($_REQUEST['srch_type']) $tolettpe = $_REQUEST['srch_type'];
$args = array(
    'numberposts'  => $latestcount,
    'category'     => $catidstr,
    'meta_query' => array(
        array(
            'key' => 'property_type',
            'value' => $tolettpe,
            'compare' => 'LIKE'
        )
    )
);
$posts = get_posts($args);

The generated query puts the search term between two % signs, so there is no need to add any in the code.

Leave a Comment