Support

Account

Home Forums General Issues How can I display a specific field value from a linked(relationship/object)post?

Helping

How can I display a specific field value from a linked(relationship/object)post?

  • Hi there – trying to figure out how to do this. Say I have two post types – “Company” and “Product”. What I want to do is have a field (probably a relationship or post object) referencing a specific field from the linked/relationshipped company post (say “company_status” for the sake of this example) and displays the (non-editable) value along with all of the other editable acf fields for that post. Here’s the for instance:

    Say that we have 4 companies – A, B, C, and D. Company A and B have a status of “Open”, C has a status of “Closing”, with D having a status of “Closed”. We also have 5 products – 1, 2, 3, 4, and 5. Products 1 and 2 are made by Company A, 3 by B, 4 by C, and 5 by D. What I would LIKE/expect to have show up here is that product 1 and 2’s “company_status” field would display “Open”, 3 would display “Open”, 4 would display “Closing”, 5 would display “Closed”.

    I’ve tried looking at the bi-directional relationship plugin by John H but it’s not quite making sense to me – but then again post object and relationship fields don’t fully make sense for me either at the moment.

    Really appreciate it and thanks in advance!

  • Are you saying that, given your example, in the Edit Product screen for “Product 1” there would be a field with the text “Open”? And this field is set to the company_status field of an associated company?

    If that’s the case, I would do something like this:

    1. Associate Products to Companies (already done).
    2. Create a “company_status” field on Company cusotm post type.
    3. Create a “product_company_status” field on Product custom post types.
    4. Use the update_value hook to detect the Company’s company_status field getting updated. See the Update_value() documentation.
    5. In the hook function, find all products that are associated with that company. Then loop over all Products and update the “product_company_status” field with the new “company_status”. See the update_field() documentation.

    Now whenever a “Company” post’s has it’s company_status value updated, the hook function will fire, updating all products. I hope that makes sense.

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

The topic ‘How can I display a specific field value from a linked(relationship/object)post?’ is closed to new replies.