DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Ever wanted to be able to only get the list of posts which have a specific custom field key as well as a specific value? If yes, just read on. Our friend John Kolbert have this short, but useful recipe for you.

To achieve this recipe, simply find the loop and add the query_posts() function just above, as in the example below:

<?php query_posts('meta_key=review_type&meta_value=movie');  ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

You’ll get the list of post having review_type as a custom field key and movie as a value. Just change theses values to fit your needs.

Credits goes to John Kolbert for this great piece of code!


  1. Loving this. Saved me a lot of work. Just one question. How can I get posts filtered by two custom fields?
    I`m building a theme for a website and I need to display on the front page some articles that have two custom fields, and only those articles. New articles will be added so filtering them by ID isn`t an option.
    Thank you and keep up the good work!

  2. I am having custom field ‘city’ for multiple posts,
    and I need to display distinct city names with posts count,
    e.g:London (13)
    Paris (10)
    where 13 and 10 are number of posts means there are 13 posts having custom field ‘city’ value as ‘London’.
    what will be the query?

    $query = new WP_Query(array(‘post_type’=>’post’,’cat’=>’17’,’meta_key’=>’City’,’groupby’=>’meta_value’));
    did not worked.

  3. Thanks. I was looking for either get_posts or WP_Query to count a specific post type with a specific custom field value. This solved my problem.

    One thing: Looking at hira’s post, he used groupby when he should have used meta_value.

    @Hira: If you see my comment, try changing it.


  4. What should I do if I wanted to recover those post that had more than one custom field value?

    For example: Cities with parks, airports, and beach. (just an example)

  5. Love it! bookmarked and link to it. thanks. (so simple)

  6. One point, so you have any such code which can find out a custom post having a specific values in multiple custom fields

Leave a Comment

Your email address will not be published. Required fields are marked *