Support » Fixing WordPress » HELP: Query Posts by Custom Field Meta Value

  • Resolved kbellagio

    (@kbellagio)


    Hello,

    I searched through the forums and found some solutions but I was unable to solve my problem.

    The Code

    <?php $winething = get_post_meta($post_id, "wine type", true);
    query_posts('post_type=wines&orderby=title&order=DSC&meta_value=' . $winething); ?>

    What I am trying to do
    The code above shows what I am trying to do, use a custom field value in the key ‘wine type’ to filter my query posts.

    What is happening
    All the custom posts are being displayed as if they all have the same custom field value(they do not).

    Thank you for your help in resolving this matter.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    the $post_id ($post->ID) is usually not set before the loop. The get_post_meta() is not working without an ID or do you set the $post_id some other way?

    Thanks for your reply keesiemeijer.

    I was thinking the same thing. The query posts is for a sub navigation on the post’s page.

    I guess I just needed a quick brain jab to rethink the problem. I did a quick google search and found a thread on here. For those wanting to know:

    <?php
    if ( is_single() ) {
    global $wp_query;
    $post = $wp_query->post;
    $winething = get_post_meta($post->ID, 'wine type', true);
    }
    query_posts('post_type=wines&orderby=title&order=DSC&meta_value=' . $winething); ?>

    The above code works.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘HELP: Query Posts by Custom Field Meta Value’ is closed to new replies.