【问题标题】:Wordpress get posts[relationship] from usersWordpress 从用户那里获取帖子[关系]
【发布时间】:2019-01-22 07:43:02
【问题描述】:

我有一个案例,其中我有 2 种自定义帖子类型,假设它们是项目和团队。使用高级自定义字段,我在创建项目时有一个关系字段,我正在分配哪些团队正在处理它。 (重要提示:团队帖子类型中没有自定义字段)。稍后,我想在单团队中列出该团队正在从事的所有项目。

高级自定义字段的示例是您在团队中有一个自定义字段,而不是在项目中,我需要做相反的事情。 (这里是 acf 文档的https://www.advancedcustomfields.com/resources/querying-relationship-fields/)。

我试过这样做,但它不起作用,它说我没有发布正确的数据。

$team_id = get_the_ID(); 

$posts = get_posts(array(
    'post_type' => 'projects',
    'orderby'   => 'teams',
    'post__in'  => $team_id,
));

【问题讨论】:

  • 我猜你必须在数组中包含团队的元查询,因为目前orderby 不知道teams 是什么。另外,我建议使用posts2posts 插件,就帖子类型与帖子类型的关系而言,它比 ACF 更好。

标签: wordpress advanced-custom-fields


【解决方案1】:

由于 ACF relationship 字段 位于 Project custom post type 您的single -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',
        )
    )
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 2016-12-24
    • 2023-03-18
    相关资源
    最近更新 更多