在我的案例中,我有2种自定义帖子类型,假设它们是项目和团队。使用高级自定义字段,我在创建项目时会有一个关系字段,并分配正在处理该项目的团队。 (重要:团队发布类型中没有自定义字段)。稍后,我想在一个团队中列出该团队正在从事的所有项目。
“高级自定义字段”具有的示例是您在团队中而不是在项目中拥有一个自定义字段的地方,我需要做相反的事情。 (这是https://www.advancedcustomfields.com/resources/querying-relationship-fields/的acf文档的方式。)
我尝试这样做,但是它不起作用,它表示我没有发布正确的数据。
$team_id = get_the_ID();
$posts = get_posts(array(
'post_type' => 'projects',
'orderby' => 'teams',
'post__in' => $team_id,
));
答案 0 :(得分:1)
由于 ACF 关系 字段位于您的单个项目的项目自定义帖子类型中-team.php 文件(您要在其中列出团队成员从事的项目),可以执行以下操作:
$args = array(
'post_type' => 'PROJECTS CPT NAME HERE',
'post_status' => 'publish',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ACF RELATIONSHIP FIELD NAME HERE',
'value' => get_the_ID(), // the ID of the member
'compare' => 'LIKE',
)
)
);