WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. rugbert
    Member
    Posted 4 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?

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

  2. MichaelH
    Member
    Posted 4 years ago #

    You'll need to use wpdb for that.

    See the example there:
    http://codex.wordpress.org/Function_Reference/wpdb_Class#Examples_4

  3. rugbert
    Member
    Posted 4 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) {
        $post=get_post(intval($id));
        setup_postdata($post);?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      }
    }
    ?>

    in the loop?

  4. MichaelH
    Member
    Posted 4 years ago #

    Actually that is 'the loop' replacement.

  5. rugbert
    Member
    Posted 4 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

Tags

No tags yet.