
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;
?>
7 Responses
Thanks :p
so, it is possible to count the number of posts are builded?
For example, have the count and after the title like:
- Soft custom field
Wordpress value = 2 posts: “title one”, “title two”
possible? :p
bye
I’ve been using custom field for a while now, and I really enjoy it. But, since I found this I think it will be more fun in using custom field. I think I can use it to make related content
. Thanks
I have a problem with this code. It works great on the front page, but when diving down in a post, the whole list disappears, as if it can’t find anything.
@Ronny-André: I guess this is because you have to re-initialize the query. Try to ad rewind_posts() before the code.
Thanks for the tip, but I solved it with running a custom query_posts(). I have two custom queries in the sidebar, plus the loop, but I will remember rewind_posts() for later!
Trackbacks: