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.

Need help querying for posts with either one or another custom field.

Not sure how to use the 'or' operator here, but something like this the blow -

Have tried these:

$wp_query->query('post_type=myposttype&meta_value=X||Y||Z');
$wp_query->query('post_type=myposttype&meta_value=X||meta_value=Y||meta_value=Z');
$wp_query->query('post_type=myposttype&meta_value="X"OR"Y"OR"Z"');
share|improve this question

1 Answer 1

Your example is a bit hard to understand, as it seems to query for meta_values of X,Y,Z,AB,ID,IA. I hope you can still make sense of the below posted code:

// query posts that have on of the custom values for the custom meta key 'my_key' 
// and/or one of the custom meta values for the meta key 'your_key'
$posts = get_posts( array(
     'post_type' => 'my_post_type'
    ,'meta_query' => array(
         array(
             'key' => 'my_key'
            ,'value' => array( 'AB', 'ID', 'IA' )
            ,'compare' => 'IN'
         )
        ,array(
             'key' => 'your_key'
            ,'value' => array( 'AB', 'ID', 'IA' )
            ,'compare' => 'IN'
     )
) );

Other comparison values would be 'LIKE', 'NOT LIKE', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' where the later twos are reserved for numeric comparison and need an extra meta key/value pair: 'type' => 'numeric'.

share|improve this answer

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.