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 create a search results listing for a custom post type and the problem I'm running in to is with the way the 'meta_query' parameter is stringing the passed values together. It appears to be treating each 'meta_query' value as an 'AND' rather than 'OR'. Here is the code I'm using:

$strsearch = 'lorem';
$args = array(
    'post_type' => 'products',
    'meta_query' => array(
        array('key' => '_product_make','compare' => 'LIKE','value' => $strsearch),
        array('key' => '_product_model','compare' => 'LIKE','value' => $strsearch)
        )
    ,'s' => $strsearch);
$resource_query = new WP_Query($args);

When I search anyone of the fields individually I get the desired results. When I use them together it comes up as not found since the string I'm searching for may only occur in one of the three fields listed below. Is there any way to have 'meta_query' treat each of the passed field comparisons as 'OR'?

share|improve this question
    
Great question! –  John P Bloch Jun 30 '11 at 11:45
add comment

1 Answer

up vote 6 down vote accepted

Yes, since WP 3.2 you can set the 'relation' arg:

'meta_query' => array(
    'relation' => 'OR',
    array('key' => '_product_make','compare' => 'LIKE','value' => $strsearch),
    array('key' => '_product_model','compare' => 'LIKE','value' => $strsearch)
)

See http://core.trac.wordpress.org/ticket/17011

share|improve this answer
    
Is it possible to do now? I've looked at the ticket and it seems to say "solved", but I can't figure out what to add to make it work. Would really appreciate any help. Thanks :) –  dashaluna Jun 30 '11 at 10:38
1  
Updated answer. –  scribu Jun 30 '11 at 11:09
    
thanks! –  dashaluna Jun 30 '11 at 11:31
    
@dashaluna: You're welcome. Note that on this site, the appropriate response is to upvote the answer. :) –  scribu Jun 30 '11 at 11:40
    
when i use this it seems to not search the normal fields? Seems 's' => $strsearch is ignored completely but 'relation' => 'OR' works perfectly with the meta_keys... Is it possible to search the default fields too? (title etc) –  daniel Crabbe Jul 24 '12 at 14:45
add comment

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.