Post Pic

How to: Display post based on custom fields with a custom query

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

Feb 21 2009 00:50

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!

Feb 21 2009 09:17

@Dami: Using ‘LIMIT 10′ in the query will limit the results to 10. But paginating results is a lot harder :(

Feb 23 2009 19:35

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

Feb 24 2009 17:50

@ Dami

Pagination is achieved rather easily with a readily available plugin.

http://wordpress.org/extend/plugins/wp-page-numbers/

Mar 03 2009 03:25

@ Jacob
Can you show us an example of what you mean?

Mar 03 2009 03:34

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;

Mar 03 2009 03:36

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;

Mar 03 2009 03:36

Thanks but what i mean is how it looks in an actual single.php file for example… i’m a dummy like that!

Mar 03 2009 03:40

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.

Mar 03 2009 04:56

thanks

Mar 05 2009 12:31

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.

Apr 30 2009 10:27

That’s cool. What i’m looking for. Thank you

Jun 16 2009 03:12

well i hope you will show me how to create a pagination for this custom query page, maybe in the next post. Great Thanks

Jul 09 2009 12:06

Worpresss is amazing, just to finish wprecipes post, you can fix/create pagination with this tip: http://wordpress.org/support/topic/232627

Aug 20 2009 07:50

nice info, i’ve ben hard wordk with wordpress custom fields. you help me so much. thanks.

Jan 01 2010 14:40

Jacob, and if I want to throw all the custom fields with equal values?
I should do.
Thank you.

Mar 06 2010 05:37

This is great! I’ve just used it in my latest theme I’m developing. Thanks!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required