
Many readers asked me how to be able to display posts based on custom fields values. If you’re interested to know about it, just read on, I’m going to tell you how to do it!

Many readers asked me how to be able to display posts based on custom fields values. If you’re interested to know about it, just read on, I’m going to tell you how to do it!
The first thing to do is to create a page template.
The code below will display your posts, based on the following condition: Post must have a custom field with key tag and value email. Of course, you can change it in the query to make the code fits your needs
<?php
/*
Template Name: Custom query
*/
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'tag'
AND wpostmeta.meta_value = 'email'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
if ($pageposts):
foreach ($pageposts as $post):
setup_postdata($post);
// Display your post info. For exemple: the_title();the_exerpt();
endforeach;
endif;
?>
Happy coding
21 Responses
I’ve been using this method for a while now, but I get a really long list of posts (over 200!) in the same page.
Is there a way to paginate the results using this method? I mean, show only 10, and then the next 10 and so on.
Thanks a lot!
@Dami: Using ‘LIMIT 10′ in the query will limit the results to 10. But paginating results is a lot harder
Why don’t you just use query_posts(‘meta_key=color&meta_value=blue’); ???
More info: http://codex.wordpress.org/Template_Tags/query_posts#Custom_Field_Parameters
@ Dami
Pagination is achieved rather easily with a readily available plugin.
http://wordpress.org/extend/plugins/wp-page-numbers/
@ Jacob
Can you show us an example of what you mean?
Let’s say you have a custom field called “thumb” that you use for thumbnails and you want to show all the posts containing thumbnails:
query_posts(‘meta_key=thumb’);
while (have_posts()) : the_post();
–> loop
endwhile;
Or let’s say you have a custom field called “price” that you use it to store the price of items and you want to get all the items that are less than $1000:
query_posts(‘meta_key=price&meta_compare= loop
endwhile;
There was an error with the second example because of an html character I used. Here’s the correct code:
query_posts(‘meta_key=price&meta_compare=<=&meta_value=1000′);
while (have_posts()) : the_post();
–> loop
endwhile;
Thanks but what i mean is how it looks in an actual single.php file for example… i’m a dummy like that!
Exactly like what I sent you. Find the line:
if (have_posts()) : while (have_posts()) : the_post();
And just change the “if (have_posts()) : ” by the “query_post” statement.
Then remove the “endif;” after the “endwhile;” statement.
thanks
I’ve been playing with that method for quite some time. I’ve never found it THAT useful but it’s a nice little trick to know. Thanks for sharing it.
That’s cool. What i’m looking for. Thank you
well i hope you will show me how to create a pagination for this custom query page, maybe in the next post. Great Thanks
Worpresss is amazing, just to finish wprecipes post, you can fix/create pagination with this tip: http://wordpress.org/support/topic/232627
nice info, i’ve ben hard wordk with wordpress custom fields. you help me so much. thanks.
Jacob, and if I want to throw all the custom fields with equal values?
I should do.
Thank you.
This is great! I’ve just used it in my latest theme I’m developing. Thanks!
Trackbacks: