Support » Fixing WordPress » query_posts using multiple fields and values

  • Resolved Mort


    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’ );

Viewing 4 replies - 1 through 4 (of 4 total)
  • You’ll need to use wpdb for that.

    See the example there:

    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?

    Actually that is ‘the loop’ replacement.

    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘query_posts using multiple fields and values’ is closed to new replies.