如何通过sql查询获得多元meta_value的post_id

时间:2013-04-11 05:29:27

标签: wordpress

如果有帖子,

id = 10

meta_key = size
meta_value = xxl

meta_key = color
meta_value = red

如何使用 sql 来获取此post_id

$my_post_id = $wpdb->get_results($wpdb->prepare( " SELECT post_id FROM $wpdb->postmeta WHERE meta_value = 'xxl' "),'ARRAY_A');

1 个答案:

答案 0 :(得分:0)

您的基本查询将是

SELECT post_id 
  FROM wp_postmeta 
 WHERE meta_key   IN ('size', 'color')
   AND meta_value IN ('xxl', 'red')
 GROUP BY post_id
HAVING COUNT(DISTINCT meta_key)   = 2 
   AND COUNT(DISTINCT meta_value) = 2

这是 SQLFiddle 演示