2-way relationship | WordPress.org

[ad_1]

Hi @itapronk! The first part of answering this is to figure out what makes the most sense regarding your data structure. That’s a question for the ACF forum and isn’t L&L-specific, since you can use L&L to loop through just about any data on your WordPress site. That being said, since you’re here, I’ll give it a shot to help provide some options.

ACF has a relationship field, but the limitation is that it creates a one-way relationship between posts, not two-way as you’re suggesting you’d like. There’s some official documentation about setting up a two-way relationship field as well as ACF add-ons to enable that.

The three approaches I see for you here are:

  1. Set up a bidirectional relationship field and then use L&L to loop through it. I’m not exactly sure how the data is saved in those types of relationship fields so you’ll have to do some tinkering, but you might try using the ACF relationship field loop syntax like <Loop acf_relationship=your_field_name> for starters. If that doesn’t work, you might try a field loop like <Loop field=your_field_name> or a post loop like <Loop type="locations,trainers" id="{Field your_field_name}">.
  2. If you can’t set up a bidirectional relationship field with ACF or some other tool, then you could probably still manage this using a regular one-way ACF relationship loop. Let’s say you set up a relationship field on your ‘locations’ post type called trainers_at_this_location. The Loops & Logic template on your locations post type would just be a standard acf_relationship loop (docs linked above). Then if you wanted to place an L&L template on the trainer’s page, you could use syntax like this, which only loops through locations where the trainers_at_this_location field includes the post ID of whatever current trainer you’re looking at: <Loop type=locations field=trainers_at_this_location field_compare=includes field_value="{Field id}">. This method involves the least additional work/setup/plugins but it might be a little bit trickier to understand what’s going on if you’re just starting out with L&L.
  3. The third way would just be to make two separate ACF relationship fields and loop through them with two separate acf_relationship loops (linked above). This is probably the easiest to visualize but would be more annoying for you to manage from a data entry perspective.

Hope that’s helpful! If you ever get stuck or need more help, the Loops & Logic forum is a pretty active community and there are a lot more people that browse that who can help you compared to this WordPress forum, so you’ll probably get better/faster support there.

Best of luck with building your template!

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer