创建查询您必须使用meta_query
parameter.
您的关系字段作为序列化数组保存在DB中,因此,在meta_query
参数应使用LIKE
操作人员
假设CTPhotel
只有一个city
分配后,查询将如下所示:
$cities = get_field(\'contact_city\');
if ( is_array($cities) && count($cities) ) {
$city_id = current($cities)->ID;
$args = [
\'post_type\' => \'hotel\',
\'meta_query\' => [
\'key\' => \'contact_city\',
\'value\' => \'"\' . $city_id . \'"\',
\'compare\' => \'LIKE\',
],
// ...
];
$my_query = new WP_Query( $args );
}
如果发布
hotel
有几个
city
已分配,并且您希望显示从分配给当前查看的酒店的所有位置到酒店的链接,
meta_query
将更改如下:
$args = [
\'post_type\' => \'hotel\',
\'meta_query\' => [
\'relation\' => OR,
[ /* \'key\', \'value\', \'compare\' */ ],
[ /* \'key\', \'value\', \'compare\' */ ],
],
Sample code:
$meta_query = [ \'relation\' => \'OR\' ];
foreach( $cities as $city )
{
$meta_query[] = [
\'key\' => \'contact_city\',
\'value\' => \'"\' . $city->ID . \'"\',
\'compare\' => \'LIKE\',
];
}
$args = [
\'post_type\' => \'hotel\',
\'meta_query\' => $meta_query,
];
$my_query = new WP_Query( $args );