[resolved] query_posts using multiple fields and values (5 posts)

  1. Kyle Jennings
    Posted 5 years ago #

    Say I wanted to filter posts not with one key and value, but a few of them, how would I go about doing that?

    query_posts('meta_key=color&meta_value=blue'); Just does one key and value, what if I wanted to filter by color AND shape?

    query_posts('meta_key=color&meta_value=blue' AND 'meta_key=shape&meta_value=triangle' );

  2. MichaelH
    Posted 5 years ago #

    You'll need to use wpdb for that.

    See the example there:

  3. Kyle Jennings
    Posted 5 years ago #

    cooool. I just started learning SQL but Ive got enough down for my query...

    where do I put this part tho?:

    if ($postids) {
      echo 'List of ' . $meta_key3_value . '(s), sorted by ' . $meta_key1 . ', ' . $meta_key2;
      foreach ($postids as $id) {
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>

    in the loop?

  4. MichaelH
    Posted 5 years ago #

    Actually that is 'the loop' replacement.

  5. Kyle Jennings
    Posted 5 years ago #

    AWESOME! Ok I almost have it. For some reason tho, I get no results when I add something like this

    AND post_id IN (SELECT post_id
    FROM wp_postmeta
    WHERE ( meta_key = 'Utilities'
    AND meta_value = '%Water%' ))

    works fine from terminal, but not from the code you linked me to. As long as thats not in there I get search results, but as soon as I add that line (or another for a different key/value) I get nothing even tho I know the entry exists.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.