“我的SQL不工作”问题很难回答,因为如果没有OP的数据集,它们几乎是不稳定的,但是。。。
我想你可能把事情弄得太复杂了。
SELECT
DISTINCT (p.ID),
p.post_title
FROM wp_posts p
JOIN wp_postmeta pm1 ON p.ID = pm1.post_id
JOIN wp_postmeta pm2 ON p.ID = pm2.post_id
WHERE 1 = 1
AND p.post_type = \'game\'
AND (
pm1.meta_key = \'year\'
AND pm1.meta_value = \'2014\'
)
AND (
pm2.meta_key IN (\'team1\',\'team2\')
AND pm2.meta_value = \'yankees\'
)
我想这可以满足你的需要。是的,我很确定你不能用
WP_Query
直接,但是。。。
function swap_key_wpse_144950($where) {
remove_filter(\'posts_where\',\'swap_key_wpse_144950\');
$where = str_replace("= \'xxteamsxx\'"," IN (\'team1\',\'team2\')",$where);
return $where;
}
add_filter(\'posts_where\',\'swap_key_wpse_144950\');
$args = array(
\'meta_query\' => array(
array(
\'key\' => \'year\',
\'value\' => \'2014\'
),
array(
\'key\' => \'xxteamsxx\',
\'value\' => \'yankees\'
)
)
);
$q = new WP_Query($args);
var_dump($q->request);