WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

If I were to take a standard query post.

<?php query_posts('post_type=payment'); while (have_posts()) : the_post();?>

Only this time I would like to query the post by 2 custom fields that it may contain.

<?php query_posts('post_type=payment'.get_post_meta($post->ID,'bookingref', true).get_post_meta($post->ID,'customerref', true) ); while (have_posts()) : the_post(); ?>

That doesn't work. Is something like this possible and how is it done?

Any ideas?


share|improve this question
accepting answers to your previous questions would be a good idea – anu Mar 3 '11 at 17:38
up vote 9 down vote accepted

To query posts by custom fields you can use the 'meta_query' parameter

$args = array(
'post_type' => 'payment',
'meta_query' => array(
            'key' => 'bookingref',
            'value' => 'the_value_you_want',
            'compare' => 'LIKE'
            'key' => 'customerref',
            'value' => 'the_value_you_want',
            'compare' => 'LIKE'
query_posts($args); while (have_posts()) : the_post(); ?>

you can't use get_post_meta inside the query because it gets you the value and not the key and also it accepts a Post ID to get that value of and before the query $post->id is not in the scope.

share|improve this answer
yup, this is better than my (deleted) answer - meta_query is the way to go for multiple custom key querying – anu Mar 3 '11 at 17:51
Marvellous well done. You are missing a bracket though. You need .. )); query_posts($args) .. – Robin I Knight Mar 3 '11 at 18:01

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.