How to Only Show Posts With a Specific Custom Field

Sometimes you only want to show posts that you’ve added a specific custom field to. For instance, lets say you write songs and movie reviews and for each you give them a custom field “review_type” with the value set to either “songs” or “movie”. So how would you show posts that are only movie reviews? Easily!

A typical post loop begins like this:

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

We’re going to add a simple query_posts function immediately above the loop code. In our scenario it would look like this:

<?php query_posts(‘meta_key=review_type&meta_value=movie’);  ?>

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

We’ve now restricted posts in the loop to movie reviews. Easy! Read the WP Codex article for more advanced uses of the query_posts function.

One Response to How to Only Show Posts With a Specific Custom Field

  1. Muhammad Shakeel May 12, 2009 at 9:17 am #

    Wonderful article, thanks for sharing.

Leave a Reply

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