我创建了一个带有自定义字段的自定义帖子类型,需要根据几个元数据值对其进行过滤。
$args = array(
\'post_type\' => \'match_report\',
\'post_status\' => \'publish\',
\'meta_key\' => \'report_type\',
\'meta_value\' => \'cup\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'report_home-select\',
\'value\' => $team_id,
\'compare\' => \'=\',
),
array(
\'key\' => \'report_away-select\',
\'value\' => $team_id,
\'compare\' => \'=\'
)
)
);
$reports = new WP_Query($args);
基本上,我想创建一个具有OR和and关系的元查询。所以用英语我要所有的比赛报告
report_type
\'杯具
report_home-select
使用特定team\\u id的值或
report_away-select
具有相同值的键。我希望这是有意义的。本质上,我想要双重关系。
SO网友:eteich
您可以将其拆分为两个WP\\U查询:
$args = array(
\'post_type\' => \'match_report\',
\'post_status\' => \'publish\',
\'meta_key\' => \'report_type\',
\'meta_value\' => \'cup\',
\'fields\' => \'ids\'
);
$reportsPre = new WP_Query($args);
$post_IDs = array();
if(isset($reportsPre->posts) && !empty($reportsPre->posts)){
foreach((array) $reportsPre->posts as $id) {
$post_IDs[] = $id;
}
}
$args2 = array(
\'post__in\' => array($post_IDs),
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'report_home-select\',
\'value\' => $team_id,
\'compare\' => \'=\',
),
array(
\'key\' => \'report_away-select\',
\'value\' => $team_id,
\'compare\' => \'=\'
)
));
$reports = new WP_Query($args2);
我没有靠近一台可以测试这个的计算机,但它应该可以工作。