large shade tree

Navigation

Information

Recent Posts

Shade Tree Sites |Building Better Websites

Custom Field Loop / Query

This piece of code allows you to create a custom loop based on your custom fields in published posts using a direct database call:

<?php
$querydetails = "

SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'your key here'
AND wpostmeta.meta_value = 'your value here'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC
";
$pageposts = $wpdb->get_results($querydetails, OBJECT)
?>

After running the query you can use this as an example of how to output the results:

<?php if ($pageposts): foreach ($pageposts as $post):
setup_postdata($post); ?>
// Make your loop here. For example :
<div <?php post_class(); ?>> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
</div> <?php endforeach; endif; ?>

Source: CSS Tricks