WordPress Query กรณี meta_key 2 ตัว

SQL นี้ ใช้สำหรับ Query กรณีที่มี meta_key มากว่า 1

SELECT
  p.post_title,
  pm1.meta_value,
  pm2.meta_value

FROM wp_posts as p

INNER JOIN wp_postmeta as pm1
ON p.ID = pm1.post_id

INNER JOIN wp_postmeta as pm2
ON p.ID = pm2.post_id

WHERE
  pm1.meta_key = '_my_postmeta_field1'
AND
  pm2.meta_key <> '_not_this_field'

Leave a Reply

Your email address will not be published. Required fields are marked *