The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

How to use Custom Select Query with two custom fields? (4 posts)

  1. mingofaust
    Posted 8 years ago #

    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.

  2. Alex Hackbunker
    Posted 7 years ago #

    Hi, i have also de same doubt, any help?

  3. Dgold
    Posted 7 years ago #

    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.

  4. elfin

    Posted 7 years ago #

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


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

Topic Closed

This topic has been closed to new replies.

About this Topic