
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
One Response
I try to do sth like this but it doesn’t seem to work
$querystr = ”
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = ‘parent_cat’
AND wpostmeta.meta_value = ‘methods’
AND wpostmeta.meta_key = ‘main_item’
AND wpostmeta.meta_value = ‘true’
AND wposts.post_status = ‘publish’
AND wposts.post_type = ‘post’
ORDER BY wposts.post_date DESC
“;
Trackbacks: