Support

Account

Home Forums ACF PRO Bidirectional Relationships and multiple post types

Solved

Bidirectional Relationships and multiple post types

  • Hi there,

    I just read through the Bidirectional Relationships tutorial, but it doesn’t seem to accomplish what I hoped it would.

    Here’s my situation:

    I have two custom post types: Clients and Jobs. I initially created a relationship field for the Jobs post type, where the user may select to which Client this Job belongs to. Effectively this becomes a One to Many relationship: One Job belongs to One Client; One Client may have Many Jobs. So I made sure this field has a minimum and maximum of 1. (A job may belong to only one client).

    That works well enough, but I wondered how on earth I would reflect this relationship on the front-end on the Client page. I would need to somehow loop through all the Job posts that are related to this Client.

    That’s when I found the Bidirectional Relationship hook. I threw that into my functions.php, replaced the related_posts bit with my own job_belongs_to_client field name, and it doesn’t work. When I go to edit that client in the WP admin area, there’s no related field displaying. So, I tried to change the settings on my Field Group for my Jobs post type, so that it would display for either Job OR Client posts, still no dice. Well, it does still display on Client posts, but it isn’t showing the relationship it has with the Job post type.

    Does any of that make sense? Is this possible?

  • Just found Posts 2 Posts plugin. Amazing!! I think I’m set. For now. 🙂

  • Ok, I’d like to re-open this as it wasn’t solved and I’m trying to do the same thing only I really need to keep it in the ACF Pro family. I have a couple Volleyball clubs that have to update their teams and players each season. Here’s the current setup:
    Team CPT (team)
    Field

    • Players (players): Relationship field, can select multiple posts, limited to only the “player” CPT.

    Player CPT (player)
    Field

    • Team (team): Relationship field, required, can select only one post, limited to only the “team” CPT.

    I’d like to setup the bidirectional relationship to allow a player CPT to select a team CPT from it’s “team” relationship field and that team CPT then have it’s “player” relationship field updated with the new player CPT. And vice versa. I have the ability to import the massive list of players to assign to the 30+ teams but without the bidirectional option this won’t really work. From a management standpoint it’s much easier to edit a team’s list of players from the team CPT edit page. However, the CSV import allows to mass update or add players and thereby the teams they are assigned to.

    Is this possible with ACF Pro? If so, I’d be happy to write up a nice article detailing all of the setup I’ve gone through with these relationships as I’m sure others have tried to accomplish similar functionality albeit different CPT and field names. Those of course have been changed to protect the innocent. 🙂

  • This is not possible in the core ACF plugin. I have created and example of how this can be done using standard ACF relationship fields, it requires some modification to get it to work with your specific fields and shows the code needed for making the automated updating happen https://github.com/Hube2/acf-filters-and-functions/blob/master/acf-reciprocal-relationship.php

    I have also created an add one that automatically does this for all top level relationship fields https://github.com/Hube2/acf-post2post

  • So it looks like the add on only works if the field is the same name on each post whereas the acf-reciprocal-relationship.php code you shared seems to allow for two different fields on two different post types. Am I seeing/reading this correctly?

    Either way, wow. You’ve done some fantastic coding there sir.

  • Yes, I created the reciprocal relationship example for that purpose. This also really need to be fields that are not inside a repeater or a flex field as will because there’s really no way to know which row of a repeater should be updated. I suppose, with a bit of work, someone could modify that code to work, but it’s really something that needs to be worked out on a field by field basis.

    As I’ve said in other similar topics, I think the main reason that two way relationships are not is ACF is that there are simply too many possible combinations of what can be related and it would be a deep rabbit hole for the developer if he tried to add it. The code is not really that difficult if you confine yourself to a specific set of fields.

  • I totally understand. The rabbit hole can get deep, quickly. In my use case the fields are on the top level and not buried within flexible content or repeaters.

    So the only thing I need to use in my case would be the example code, right?

  • Yes, you should just need to set up the right field keys.

  • Awesome work with the plugin. I am using it right now and it works exactly as advertised! Thank you.

  • Thank you for the amazing acf-post2post plugin. This plugin should be in ACF core.

  • I have a follow up for this. So we have two post types (example: A and B) that are related to each other using a field of the same name (as described above).

    Is there a way to create a searchable dropdown filter on post type A that contains a list of titles of post type B so we can filter B posts that are linked to that particular A post?

  • You could probably use an acf/load_field filter https://www.advancedcustomfields.com/resources/acf-load_field/, check the post to see what post type it is and then alter the settings of the field to filter by a specific post type. It could be complicated figuring out what post type the field is being loaded for and I’m not sure of all the details.

    I probably would not do that much work and I’d use two different fields, one on each post type, with the correct settings.

    The first way would be more elegant, but the second would be easier to do, take less code, be faster to accomplish and be easier to maintain in the future

  • A bit more info on how to achieve this (using shawnrosspeters example) with Post-2-Post without any code tweaking:

    With ACF Post-2-Post, this can be done simply by:

    Team CPT
    – Create a custom field group for Team CPT
    – Set the field group to be visible only on Team CPT
    – Create there the field “Related Players” and give it the fieldname “players-teams”
    – Filter by Post Type = Player CPT

    Player CPT
    – Create a custom field group for Player CPT
    – Set the field group to be visible only on Player CPT
    – Create field “Related Teams”
    – Reuse the same fieldname as for Team CPT Related Players (“players-teams”)
    – Filter by Post Type = Team CPT

    * With this method, you will have a different field label for each of the 2 different custom post types.
    * The filtering will only to show results of the other CPT when selecting posts for the relationships.
    * By virtue of using the same fieldname “players-teams” in both field group fields, the Post-2-Post plugin works perfectly, ensuring that the bidirectional relationships are established.

  • Since Metabox is gaining traction extremely fast and the are offering bi-directional relationship fields (even between two CPTs) natively out of the box, I feel like quite a bunch of ACF users are switching (or are contemplating to switch) to Metabox.

    Thus, I would really love to see a NATIVE CPT to CPT bi-directional relationship feature in ACF anytime soon. Since sites are getting more and more dynamic all the time, it’s about time for ACF to launch this feature in ACF core.

    What do you all think?

  • That plugin has been around a while, it’s another option. As stated above, two way relationships have issues and are not really be done inside or repeaters, groups and flex fields. Adding this to ACF would take a major rewrite of how ACF stores data. Meta Box adds tables to the database for storing information which is something that the original developer tried to avoid.

    No one here can do anything, you need to send your request to the developers.

  • John, thanks for your response. I have already sent my request to ACF support team.

    I am aware of these limitations, but wanted to bring this to the table here too. For us here and maybe also for other users who are looking and waiting for ACF bidirectional relationships …

    And yes, I still think that this is an extremely important feature (for dynamic sites) that should be a part of ACF core, hopefully to be launched anytime soon.

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

The topic ‘Bidirectional Relationships and multiple post types’ is closed to new replies.