WordPress.org

Forums

[resolved] HELP: Query Posts by Custom Field Meta Value (3 posts)

  1. kbellagio
    Member
    Posted 4 years ago #

    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.

  2. keesiemeijer
    moderator
    Posted 4 years ago #

    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?

  3. kbellagio
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.