Show WordPress Posts With a Specific Custom Field and Value

May 14, 2009 · Print This Article

Ever wanted to be able to only get the list of wordpress posts which have a specific custom field key as well as a specific value?

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

Simply find the typical post loop and add the query_posts() function, as in the example below:

Find 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&metavalue=template');  ?>
<?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 template as a value. Just change theses values to fit your needs.

Your choice for site templates and wordpress themes


Got something to say?

You must be logged in to post a comment.