Query Posts or Get Posts by custom fields, possible?

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

<?php
$args = array(
'post_type' => 'payment',
'meta_query' => array(
        array(
            'key' => 'bookingref',
            'value' => 'the_value_you_want',
            'compare' => 'LIKE'
        ),
        array(
            '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.

Leave a Comment