您可以尝试在meta\\u查询中添加更多检查。
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => 10,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'relation\' => \'AND\', // make sure only this key exists by matching parameters
array(
\'key\' => \'key1\',
\'compare\' => \'EXISTS\'
),
array(
\'key\' => \'key2\',
\'compare\' => \'NOT EXISTS\'
)
),
array(
\'relation\' => \'AND\', // check for the opposite here
array(
\'key\' => \'key2\',
\'compare\' => \'EXISTS\'
),
array(
\'key\' => \'key1\',
\'compare\' => \'NOT EXISTS\'
)
)
)
);
如果只比较两个元键,那么这是一个确定的解决方案,但编写sql查询可能会更简洁。