0
votes

I have a case in which I have 2 custom post types, let's say they are projects and teams. Using Advanced custom fields, I have a relationship field while creating a project and I'm assigning which teams are working on it. (Important: there is no custom field in the teams post type). Later on, I want on single-team, to list all the projects that this team was working on.

The example that Advanced custom fields has is where you have a custom field in the team, not in the project, and I need to do the opposite. (Here is how acf documentation is https://www.advancedcustomfields.com/resources/querying-relationship-fields/).

I tried doing this, but it doesn't work, it says that I don't post a correct data.

$team_id = get_the_ID(); 

$posts = get_posts(array(
    'post_type' => 'projects',
    'orderby'   => 'teams',
    'post__in'  => $team_id,
));
1
I guess that you have to include in the array a meta query for the teams, as currently the orderby doesn't have an idea what is teams. Also I would recommend to use posts2posts plugin which is a way better then the ACF in terms of post type to post type relations.Ale

1 Answers

3
votes

Since the ACF relationship field is located in your Project custom post type in your single-team.php file where you want to list of the projects on which the team member worked on you can do the following:

$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',
        )
    )
);