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'm trying to see if I can make this happen with the built-in meta_query function. I'm taking a $_GET variable and want to return posts that contain that string inside the meta_key urls. So far, the below seems like it would be the most appropriate, but it's not returning values like I expected.

$search = '%' . $search . '%';
$args = array(
    'meta_query' => array(
        array(
            'key' => 'urls',
            'value' => $search,
            'compare' => 'LIKE',
        )
    )
);
$helpquery = new WP_Query($args);

Any ideas on making this work, or should I resort to a more direct $wpdb query?

Update #1

I see this being marked as a duplicate, but my question is specifically regarding whether this can be done with the meta_query or not.

share|improve this question

2 Answers 2

First of all - I assume the $search variable is being populated from $_GET (it's not in your code).

Secondly, do you need the % ? I think using LIKE with wp_query doesn't require them.

share|improve this answer
    
Yeah, it is—just didn't show it for brevity's sake. I'm not sure if I need the % in this case, but it wasn't working without them either, and I know I MySQL would normally want wildcards on both sides for this sort of thing. –  Cliff Seal Jul 11 '13 at 2:07

What about using:

$args = array(
    'meta_key' => 'urls',
    'meta_value' => $search    
);

According to http://core.trac.wordpress.org/ticket/23919 this solutions will add wildcars.

share|improve this answer
    
Thanks, but it doesn't quite work as expected. For instance, if 'module' is in the meta string I'm searching through, 'module/read' won't return a result. –  Cliff Seal Jul 18 '13 at 14:33

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.