WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is my query, it shows the posts that have meta_key as extra1 and meta_value as test

<?php $customkey1 = 'extra1'; ?>
<?php $customvalue1 = 'test'; ?>
<?php query_posts('meta_key=' . $customkey1 . '&meta_value=' . $customvalue1);  ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php the_title(); ?>

<?php endwhile; ?>
<?php endif; ?>

My question is how can I still show the posts that have extra1 as metakey and test as metavalue but also the posts that have extra2 as metakey and test2 as metavalue in the same query. A combination of two or more variables.

share|improve this question
up vote 2 down vote accepted

If you are using WP 3.1, I would recommend using the meta_query parameter with the WP_Query class.

$args = array(
    'meta_query' => array(
            'key' => $customkey1,
            'value' => $customvalue1,
            'compare' => '='
            'key' => $customkey2,
            'value' => $customvalue2,
            'compare' => '='
$query = new WP_Query( $args );

if ( $query->have_posts() ) : 
    while ( $query->have_posts() ) : $query->the_post();

The meta_query parameter allows for very powerful queries with metadata.



share|improve this answer
Hi there thanks but it seems that it doesn't working. It continuesly showing me all posts of the blog. – Mairy May 1 '11 at 1:23
If you pastebin the code, I'll take a look. Are you using 3.1? – tollmanz May 1 '11 at 1:51
My bad. Is working! Thank you! Is there a compact-simpler way to handle 10 arrays instead of having it copy-paste 10 times for different variables? – Mairy May 1 '11 at 2:01
@Mairy: if your custom keys need to be checked against the same value using the same compare setting, you could create a loop for DRY code. – Geert May 1 '11 at 6:01
Please take a look here wordpress.stackexchange.com/questions/16079/… – Mairy May 1 '11 at 6:23

Your Answer


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.