此处可能需要180°的解决方案;)再次思考该问题并尝试以下方法:
作为访客,我可以根据颜色和大小进行过滤。
因此,您的解决方案可能是:
...
JOIN ... (A)
... (B)
... (C)
WHERE (A.meta_key = \'color\' AND A.meta_value = \'red\')
OR (B.meta_key = \'color\' AND B.meta_value = \'blue\')
...
AND (C.meta_key = \'size\' AND C.meta_value = 1)
...
但实际上,您可以使用
IN
:
JOIN ... (A)
... (B)
WHERE (A.meta_key = \'color\' AND A.meta_value IN (\'red\', \'yellow\'))
AND (B.meta_key = \'size\' AND B.meta_value IN (1, 2, 3))
因此,开始使用“IN”来比较值
like in the manual:
$args = array(
\'post_type\' => \'my_custom_post_type\',
\'meta_key\' => \'age\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
\'meta_query\' => array(
array(
\'key\' => \'age\',
\'value\' => array( 3, 4 ),
\'compare\' => \'IN\',
),
),
);
$query = new WP_Query( $args );
您甚至可以开始猜测,并且只使用一个连接或根本不使用连接:
WHERE meta_key IN (\'color\', \'size\')
AND meta_value IN (\'red\', \'blue\', 123, 45)
这会很快,但可能会导致大量误报。