
Two days ago, I told you how you can easily get a list of posts having a certain custom field. One of my readers, csseur3, asked how to get a list of post having a specific custom field + a specific value. Here’s how to do it.

Two days ago, I told you how you can easily get a list of posts having a certain custom field. One of my readers, csseur3, asked how to get a list of post having a specific custom field + a specific value. Here’s how to do it.
Nothing hard with this recipe, especially if you read the How to only get posts having a specific custom field? recipe I posted here two days ago.
The code is basically the same, we just have to verify if the custom field is set and if its value is the one we expect. If yes, we display the post info.
<?php if (have_posts()) :
while (have_posts()) : the_post();
$customField = get_post_custom_values("img");
if ( (isset($customField[0])) && ($customField[0] == "myValue") ) {
//Custom field have myValue as a value, display info
the_title();
the_excerpt();
}
endwhile;
endif;
?>
Leave a Comment