26

How to query for mutiple meta key values with the same key

$querystr = "  
            SELECT $wpdb->posts.* 
            FROM $wpdb->posts, $wpdb->postmeta
            WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 

            AND $wpdb->postmeta.meta_key = 'key1'   
            AND $wpdb->postmeta.meta_value = 'value1'
            // why doesn't this work?
            AND $wpdb->postmeta.meta_value = 'value2'

            AND $wpdb->posts.post_status = 'publish' 
            AND $wpdb->posts.post_type = 'post'
            ORDER BY $wpdb->posts.post_date DESC
                ";

next code

<?php 
$args = array(
    'meta_query' => array(
        array(
            'key' => 'key1',
            'value' => 'value1',
            'compare' => '='
        ),
// this array results in no return for both arrays
        array(
            'key' => 'key1',
            'value' => 'value2',
            'compare' => '='
        )
    )
);
$the_query  = new WP_Query( $args );
 ?>

                <?php /* Start the Loop */ ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                    <?php get_template_part( 'content', get_post_format() ); ?>

                <?php endwhile; ?>

4 Answers 4

34

I feel like there's an AND/OR confusion going on here.

The queries in the OP will only return posts which have both key1 = 'value1' AND key2 = 'value2'. Most WP plugins (that I know of, anyway) do not store multiple values in postmeta, for the same post, using the same key.

If what you want is really an OR (you want to get the posts where key1 = 'value1', as well as the posts where key1 = 'value2'), then see @WhiskerSandwich's answer, using 'IN' and an array of values for the value parameter.

Alternatively, you can provide a relation parameter to `meta_query':

$args = array(
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'key1',
            'value' => 'value1',
            'compare' => '='
        ),

        array(
            'key' => 'key1',
            'value' => 'value2',
            'compare' => '='
        )
    )
);

Note that using OR as the relation for multiple meta queries using the same key is the functional equivalent of using IN and an array of values for a single one.

2
  • Thanks for this, Boone. I didn't know the "relation" param existed. Helped me out.
    – MathSmath
    Commented Mar 21, 2012 at 22:37
  • This works if you only have one key to search on. If you have two or more, you may need to use 'AND' to combine them in the relationship parameter, in which case @WhiskerSandwich's answer below is the better fit. Commented Apr 21, 2015 at 11:58
15

I had the same problem where passing multiple arrays for the same key wasn't working. Instead, just use one array, set 'value' to an array of values, and set 'compare' to IN:

<?php

$args = array(
    'meta_query' => array(
        array(
            'key' => 'key1',
            'value' => array('value1', 'value2'),
            'compare' => 'IN'
        ),
    )
);
$query = new WP_Query( $args );

?>
1

You have to alias the postmeta table for the second value:

$querystr = "  
        SELECT $wpdb->posts.* 
        FROM $wpdb->posts, $wpdb->postmeta, $wpdb->postmeta AS mt1
        WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
        AND $wpdb->posts.ID = $wpdp->mt1.post_id

        AND $wpdb->postmeta.meta_key = 'key1'   
        AND $wpdb->postmeta.meta_value = 'value1'
        AND mt1.meta_key = 'key1'
        AND mt1.meta_value = 'value2'

        AND $wpdb->posts.post_status = 'publish' 
        AND $wpdb->posts.post_type = 'post'
        ORDER BY $wpdb->posts.post_date DESC
            ";

You can also do this now since 3.1 with a meta_query:

$args = array(
    'meta_query' => array(
        array(
            'key' => 'key1',
            'value' => 'value1',
            'compare' => '='
        ),
        array(
            'key' => 'key1',
            'value' => 'value2',
            'compare' => '='
        )
    )
);
$query = new WP_Query( $args );
2
  • Hi Milo thanks for answering. The SQL returns no value. And the array is returning no value as well unless I remove the 2nd key and value from the array. So is this bugged?
    – steen
    Commented Jan 27, 2012 at 5:48
  • @steen - I'm not sure what your issue is, I've tested both methods and they are working in my install of 3.3.1. Is your key literally 'key1' and values 'value1' and 'value2'? Do you see nothing if you print_r( $the_query ); immediately after the query?
    – Milo
    Commented Jan 27, 2012 at 6:14
0

Key is key1 and values 'value1' and 'value2' tried it both text and numeric in a fresh install with twenty eleven. print_r( $the_query ); works output looks normal. Also tried key1 and key2 also doesn't work. It works as soon as I limit it to to one array. Checked with different browsers.

This however does work.

    <?php 
$args = array(
    'meta_query' => array(
        array(
            'key' => 'wtf',
            'value' => '1',
            'compare' => '>='
        ),
// this array results in no return for both arrays
        array(
            'key' => 'wtf',
            'value' => '2',
            'compare' => '<='
        )
    )
);
$the_query  = new WP_Query( $args );

 ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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