Support

Account

Home Forums ACF PRO Relationship field once removed (2nd cousin?)

Solving

Relationship field once removed (2nd cousin?)

  • Have searched all over and not found anything similar to what I would like to do, so looking for advice and maybe a little coding help.

    What I want to do it this: Have a CPT that has a relationship to another CPT, then a Post that has a relationship to the first CPT, and use the info from the second CPT.

    In detail,

    – I have a CPT named “Projects”. Each project has a three-letter code, e.g. “ABC”.

    – I have a second CPT named “Agents”.

    – Each Project has (1) Agent assigned to it, but that can change (people come and go). So in my CPT (post) for each Project, I have a relationship field that references an Agent.

    – I also have a CPT for Properties. Each Property is assigned to a Project. There are many Properties, but as each is entered the Project is a required field to be added via a Relationship field.

    The reason I set it up like this is so that I don’t have to be continually editing each Property to change the Agent when people come and go, or when a Project gets reassigned to a new Agent – it would be too many Properties to modify.

    NOW what I want to do is on the single-post page for each Property, display the Agent contact info from the Agent CPT.

    I know how to access the data from one CPT but not a once-removed CPT…..given that the Property only knows about the Project (in its custom fields) not the Agent.

    But how do I structure a query to access the postdata from the Agent CPT? Does it still pull all postdata from the Agent CPT that is related to the Project CPT, which is related to the Property CPT, even though the Agent CPT isn’t *directly* related to the Property CPT?

    I hope my question makes sense, I can provide more details if needed.

  • Just making sure I understand this.

    You have a one to one relationship from Property to Project. You specify the project when editing the Property.

    You have a one to one relationship from Project to Agent. You specify the Agent when editing editing the project.

    On the Property page you want to display the Agent.

    Let me know if I have that right or if I have the relationships wrong.

  • @John

    You have expressed that perfectly, thank you for doing a much better job of summarizing it than I did! 🙂

  • I’m going to assume that both of these relationship fields are “Post Object” and that you are returning the Post Object. Let me know if this is incorrect.

    
    // code on the property page
    // somewhere inside "The Loop"
    
    // get the project object on from the property post
    $project = get_field('project');
    $product_id = $project->ID;
    
    // get the agent object from the project post
    $agent = get_field('agent', $project_id);
    $agent_id = $agent_id;
    
    // show the title of the agent post
    echo get_the_title($agent_id);
    
  • Thanks John……yes both are “post object” ….I think this is enough to get me going, I’ll give this a shot and report back 🙂

  • Well I’m going to have to wait until a weekend to setup a sandbox site to test this whole process……I’m now struggling to get some custom field data into a hidden (gravity forms) form field and it’s not working using a ‘relationship’ to a CPT where the custom data is stored….I can access the data and display it on the Page just fine, but my function to access the very same data and store it in the hidden form field just isn’t working, so I had to go back to square-one and add a custom field on the Page itself to store the very same data…sigh…..

    Anyway I’ve jury-rigged it to work, but it’s not the elegant solution it should be using the additional two related CPTs. 🙁

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Relationship field once removed (2nd cousin?)’ is closed to new replies.