WordPress.org

Support

Support » How-To and Troubleshooting » How to use Custom Select Query with two custom fields?

How to use Custom Select Query with two custom fields?

  • I’ve looked at this codex page, but am unsure how to select posts based on the values of two custom fields, rather than one, as explained in the examples on that page.

    Here is the code I tried, which failed, probably because it doesn’t state which meta value goes with which meta key.

    Can anyone clarify, or provide correct code to do this? I want to find posts where the values $plowset and $phiset are within the range of the custom fields price_low and price_high.

    $querystr = "
        SELECT wposts.*
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = 'price_low'
        AND wpostmeta.meta_value >= ".$plowset."
        AND wpostmeta.meta_key = 'price_high'
        AND wpostmeta.meta_value <= ".$phiset."
        AND wposts.post_status = 'publish'
        AND wposts.post_type = 'post'
        AND wposts.post_date < NOW()
        ORDER BY wposts.post_date DESC
     ";

    I would be most grateful.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi, i have also de same doubt, any help?

    Neat idea, I wish I knew how to do this. I think Kafkaesqui would know how but I haven’t seen him on this forum real recently.

    I could be wrong, and there may be other errors but shouldn’t this:
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta

    be

    FROM $wpdb->posts AS wposts, $wpdb->postmeta AS wpostmeta

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to use Custom Select Query with two custom fields?’ is closed to new replies.