Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the following code:

$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_key'     => 'property_type',
  'meta_compare' => 'LIKE',
  'meta_value'   => $tolettpe.'%'
 );
$post_content = get_posts($args);

The value in the database is 'Sale||' and there are no query string variables in the request.

But the query returns no results.

If I use the exact value and no meta_compare, it works.

Any ideas how to make this work?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer
    
thanks, i've tried it but am not getting any results returned. Is there any way to print out the query being generated so I can run it directly in the database to debug? –  ranioli Jul 7 '12 at 11:39
    
ok, found the answer to that one codex.wordpress.org/… –  ranioli Jul 7 '12 at 12:11
    
actually my original method works as well, the extra % sign was the problem! –  ranioli Jul 7 '12 at 12:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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