
One of WpRecipes readers, Tomas, asked in the forum how he can only get posts that have a certain custom field. As far as I know, this question has been asked many times on several blogs and WordPress forums. Here is a solution to Tomas question.

One of WpRecipes readers, Tomas, asked in the forum how he can only get posts that have a certain custom field. As far as I know, this question has been asked many times on several blogs and WordPress forums. Here is a solution to Tomas question.
The answer to Tomas question is quite simple. What I've done is simply using a normal loop (That you can enhance with query_post() for exemple) and then, using a conditional instruction to check out if the post have the esired custom field.
<?php if (have_posts()) :
while (have_posts()) : the_post();
$customField = get_post_custom_values("img");
if (isset($customField[0])) {
//Custom field is set, display post info
the_title();
the_excerpt();
}
endwhile;
endif;
?>
Leave a Comment