• how to select two meta_value wp_postmeta?
    by the way get_row please

    $id = get_the_ID();
    $selecttwovalue = $wpdb->get_row("SELECT  PM.meta_value, DU.meta_value
    FROM $wpdb->posts AS P
    LEFT JOIN $wpdb->postmeta AS PM on PM.meta_key = 'meta_value1'
       JOIN $wpdb->postmeta AS DU on DU.meta_key = 'meta_value2'
    WHERE p.ID = '$id'");
            
            
           echo $selecttwovalue->PM; 
    • This topic was modified 4 years, 1 month ago by manoodin.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You want the postmeta rows for a specific post ID where the meta value is one of two values? For the same meta key? Post IDs are part of postmeta table, you don’t need to join in posts table for just the ID. I’m not sure what you’re really after, but here’s a query to get post meta records with meta key ‘foo’ where the value is either ‘ying’ or ‘yang’:
    SELECT * FROM wp_postmeta WHERE meta_key ='foo' AND meta_value IN ('ying','yang');

    Thread Starter manoodin

    (@manoodin)

    Thank you for responding. Two different metakeys, please.

    Moderator bcworkz

    (@bcworkz)

    SELECT * FROM wp_postmeta WHERE (meta_key ='foo' OR meta_key ='bar') AND meta_value IN ('ying','yang');

    You can generally place all desired values for both keys in the one ying/yang array if there is no ambiguity in values. It makes for faster queries this way. If there is possible ambiguity that must be eliminated, use this slower variant:
    SELECT * FROM wp_postmeta WHERE (meta_key ='foo' AND meta_value IN ('ying','yang')) OR (meta_key ='bar' AND meta_value IN ('red','blue'));

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘how to select two meta_value wp_postmeta?’ is closed to new replies.