如果有帖子,
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');
答案 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 演示