Query custom field
-
Hello
Wonder if anyone can help. I have a post containing several custom fields, and I want to display a list of posts in the sidebar which share a certain custom field with the currently displayed post.
I cam display the custom fields in the main loop using
<?php foreach(get_post_custom_values('Custom Field Name') as $value) { echo $value; }?>
But I’m not sure how to query a custom field within the sidebar. Here’s what I currently have, based on some research in the forums:
$catid = '2'; // set to category ID #
$customkey = 'custom-key'; // set to your custom key
$customvalue = 'custom-value'; // set to custom value, which should be generated automatically based on the value in the current postquery_posts("cat=$catid&showposts=-1");
while(have_posts()) : the_post();
$value = post_custom("$customkey");
if($customvalue == $value) : $more = 0;
?><div class="list"><img src="<?php the_excerpt(); ?>" /></div>
<?php endif; endwhile; ?>
Can anyone help? I can provide much more info if required…
Cheers
Matt
- The topic ‘Query custom field’ is closed to new replies.