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.

Previous articleDisplay your number of tweets in full text mode on your WordPress blog
Next articleShow related posts without a plugin



Please enter your comment!
Please enter your name here