• Resolved the_fear66

    (@the_fear66)


    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 post

    query_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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    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.

    Okay, there’s several elements to what you want to do here.

    a) Get the custom field from the currently displayed post. I assume you only want to show this stuff on single post pages? Doesn’t make much sense if try it on several displayed posts at once.

    b) Make a new loop to go through your posts and look for the same custom field.

    c) Display the titles and links of those posts.

    Part A means that while you’re initially displaying the post in the first place, you need to get the custom field and save its value. It also means that your main Loop to display the post must be before your sidebar occurs. This is ideally what you want anyway, so lets just assume its true for now. 🙂

    So in your main loop of single.php (or index.php, or wherever your loop is), you save off the value.
    $currentvalue = post_custom("YourKey");

    Now, in your sidebar, first we want to only display it on single pages:
    if (is_single()) {

    Next, we build a new loop to get the relevant posts:
    query_posts('showposts=-1');
    This actually gets all the posts in the blog. You can filter it by category too, if you like.

    Next, you do your loop:
    while(have_posts()) {
    the_post();
    if (post_custom("YourKey") == $currentvalue) {
    // display whatever you like, like the_title(), the_permalink(), etc
    }

    Finally, end your is_single check:
    }

    And that’s the type of thing you’ll have to do.

    Of course, if your sidebar comes *before* your actual post in the page, well, then you have a problem.

    Thread Starter the_fear66

    (@the_fear66)

    otto42 – you’re a beautiful man – it now works a treat. thanks very much.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Query custom field’ is closed to new replies.