我有两个类似以下的WP\\U查询:
搜索依据s
:
$query1 = new WP_Query([
\'post_type\' => \'product\',
\'s\' => $searchStr
]);
并通过metabox进行搜索:
$query2 = new WP_Query([
\'post_type\' => \'product\',
\'meta_query\' => [
[
\'key\' => \'my_meta_key\',
\'value\' => $searchStr,
\'compare\' => \'LIKE\'
]
]
]);
如何创建一个查询,通过“或”将这两个查询关联起来,例如:
`$查询1或$查询2“?
SO网友:Tanmay Patel
希望此代码可以为您工作。
$query1 = new WP_Query( array(
\'post_type\' => \'product\',
\'s\' => $searchStr
));
$query2 = new WP_Query( array(
\'post_type\' => \'product\',
\'meta_query\' => array(
array(
\'key\' => \'my_meta_key\',
\'value\' => $searchStr,
\'compare\' => \'LIKE\'
)
)
));
$result = new WP_Query();
$result->posts = array_unique( array_merge( $query1->posts, $query2->posts ), SORT_REGULAR );
$result->post_count = count( $result->posts );