Get post having a specific custom field and a specific value

December 26, 2008 · Print This Article

How to get a list of post having a specific custom field and a specific value?

Here’s how to do it.

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;
?>


Your choice for site templates and wordpress themes

Comments

Got something to say?

You must be logged in to post a comment.