• Resolved Ita Pronk

    (@itapronk)


    Dear all,

    I have 2 custom post types: Locations and Trainers. I am using ACF, CPT UI, Loops & Logic and Tangible.

    I want both CPT to refer to each other dynamically: on a Single Page Location (Locations), there should be a list of links to the Single Pages of the Trainers teaching at the specific location.

    What does this loop look like? Do I loop on the CPT Trainiers and then have to specify an attribute that sets the location name dynamically?

    Preferably a change is made at 1 location, after which it is also changed at the other CPT. So: if a trainer is going to teach in Nijmegen as well as Velp, this only needs to be indicated at the CPT Trainer. On the Single Page of Nijmegen, this trainer will then automatically appear. Is this possible?

    Hope you can help me!

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • 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!

    Thread Starter Ita Pronk

    (@itapronk)

    Dear Ben,

    Thank you very much for your answer!! I will work with it to see which of the 3 methods works best for me.

    One small additional question: I get back at <Field trainers_at_this_location />: [“993”]. This is trainer Desiree’s ID number. How can I get her name displayed there instead of the ID number? Please see https://rcvry.lekkerderuimte.nl/test/

    Thanks a lot!!

    Cheers,
    Ita

    Hi Ita, you sure can! This is just down to how WordPress natively saves data. If you’re associating one post with another (like a location with a trainer, in your case), WordPress isn’t going to save all the data about the trainer alongside the location post since that would make WordPress’ database unnecessarily big. It just saves the post ID of the trainer and then you can use that to get more info about the trainer as necessary.

    If your trainers_at_this_location field is an ACF relationship field, then you could grab that with a relationship field loop like this:

    <Loop acf_relationship=trainers_at_this_location>
      <Field full_name />
    </Loop>

    If that data is created in some other way other than with ACF, you can do it in a slightly more ‘manual’ way like this where you’re passing Desiree’s ID number to the id parameter of a post loop for your trainer custom post type:

    <Loop type=trainer id="{Field trainers_at_this_location}">
      <Field full_name />
    </Loop>

    Hope that makes sense!

    Thread Starter Ita Pronk

    (@itapronk)

    Hi Ben,

    Thanks a lot! Both options showed the name(s) of the trainer(s)!

    Have a nice day!
    Ita

    Thread Starter Ita Pronk

    (@itapronk)

    Hi Ben,

    I now have the names of the trainers visible on the single page of the location.

    I would now like to make the name(s) of the locations where the trainer teaches visible the other way around on the single page of the trainer as well. I have created this loop:

    <Loop type="rcvry-locatie" >
       <If acf_relationship="{Field trainers_op_deze_locatie}" includes id="current_id">
      Gives training in: <Field title /> <br />
         <Else>Something went wrong with this loop for <Field title />...<br /><br /></Else>
      </If>
    </Loop>

    Is the current_id here OK? Or should I work with Set and Get?

    Or is what I am trying to achieve not possible at all?

    BTW: ACF sent me the following code for bi-directional relations, but I don’t know how to convert this to L&L language…. https://www.advancedcustomfields.com/resources/querying-relationship-fields/

    I very much hope it is a small effort for you to help me! Cheers,
    Ita

    Thread Starter Ita Pronk

    (@itapronk)

    Hi Ben,

    It occurred to me this afternoon that it might also be possible through the categories! I have now included all provinces and places as categories, and enter ‘category=”current”‘ in the loop. This also works! \0/

    Only, with multiple places as a result, I would like to have a comma between them, and with 1 place as a result, no comma.

    Cheers,
    Ita

    PS Should the foregoing thinking be explained briefly, I am still very interested, hooray!!!

    Hi Ita, I’m glad to hear you’ve got that all working! I figured I’d chime in with a couple of pointers for you. Regarding your little template two posts up, you might try putting your text content outside the loop to make sure it doesn’t get repeated each time the loop runs in instances where a trainer works at multiple locations. Something like this on the trainer’s page:

    <p>
    The trainer <Field title /> works at the following locations: 
      <Loop type="rcvry-locatie" field="trainers_op_deze_locatie" field_compare="includes" field_value="{Field id}">
       <Field title /><If not last>, </If>
      </Loop>.
    </p>

    You can see how in this example instead of filtering my loop by nesting an If tag inside it (as you did in your template), I’ve instead used the field attribute. You can read more about the different ways of filtering the loop here. You also asked about how to make a ‘smart’ comma-separated list, so I’ve shown you how to do that in the example above with <If not last>, </If>.

    Hope that’s helpful, let me know if that works for you and whether you have any other questions! Also, since we’re sorta getting sidetracked from the original topic of this thread, you might want to post any additional questions on the community forum since there are more people with different skill sets that browse that forum and you might get better/faster answers.

    Thread Starter Ita Pronk

    (@itapronk)

    Hi Ben!

    Thanks a lot!! Your update is very helpful!!
    I’m so happy with Loops and Logics (and you!!)!

    Have a wonderful day!
    Ita

    Thanks Ita! Glad to hear you’re happy with Loops & Logic and with the support we’re working hard to provide for this free plugin. If you’re interested, it would be awesome if you’d be willing to give the plugin a rating, we’re getting pretty close to a 5-star average rating it helps us spread the word about the plugin. If you’re not interested, that’s alright, don’t hesitate to post on the L&L forum if you need help with anything else ??

    Thread Starter Ita Pronk

    (@itapronk)

    I like to give a positive review for your plugin so more people will use it! I gave you 5 stars, of course.

    Hopefully there will be more tutorials and maybe video explanations too! Would be great!!!

    Have an awesome day!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘2-way relationship’ is closed to new replies.