Question about ACF Relationship loop

I have two CPTs, say Make and Model. Each CPT has a relationship field called make_model

To show, say related models, I create this loop:

<Loop acf_relationship=make_model orderby=title order=asc>
    <Field title />
  </Loop>

How does that know to show the title from the Models CPT and not the title from the Makes CPT?

Is there a context, from which that Loop must be executed to ensure showing the correct title?

I ask because what worked for me before has now stopped working and I need to fix it.

Thanks, Richard

If I’m not mistaken, L&L markup always gets its context either from the current post/page or the current loop. So if you placed that acf_relationship loop on the post page for a particular Make, it would display the make_model relationship fields of that Make, presumably displaying the titles of whatever models are associated with it in your relationship custom field. If you placed the exact same markup on a Model post, the acf_relationship loop would inherit its context from that Model post instead and display the relationship fields of that Model (the title of the Make associated with it, I assume).

Alternatively, you could also tell L&L where it should get context by nesting loops. If that markup you wrote was inside a post loop, the inner loop would get its context from whatever post is currently being looped through. I’m not sure what your data structure is or what your goals are, but it could look like this if you wanted to display all your Make posts and then, for each one, display the Models associated with it through your relationship field.

<Loop type=make>
  <h2><Field title /></h2>
  <ul>
    <Loop acf_relationship=make_model orderby=title order=asc>
      <li><Field title /></li>
    </Loop>
</ul>
</Loop>

Disclaimer: there are a ton of options with ACF and I’m not an expert with that, so there might be a simpler or more efficient way of achieving what you’re trying to achieve. But as far as how the Loop tag gets its context, this should be correct.

Thanks very much. L&L gets confused when you create a relationship loop inside of a relationship loop.

I think the workaround is to create a relationship loop and send each post to another template that creates a single loop for that post only, then add relationship loops inside that.

Seems kinda loopy in every sense of that word.

Cheers, Richard

Hahaha that does indeed sound loopy! :laughing: But luckily I don’t think your workaround is necessary. I just tried to create a relationship loop inside a relationship loop and it worked as expected. The outer relationship loop displayed fields from the current post and the inner relationship loop displayed fields from the current item in the outer loop. I’m a bit confused about your data structure and what exactly you’re trying to achieve here, but I can at least confirm that you can create nested ACF relationship loops. No workarounds needed.

If you can share any other info about your data structure, maybe with some examples of the code you’re using and the expected/actual output, I might be able to help figure out what’s actually going on here.

1 Like

Thanks for asking. I can flesh it out a bit more. Let’s say I have a CPT for book Genres, a CPT for Authors, and a CPT for Books. They are all connected to each other with ACF Relationship fields. I’m working with Beaver Builder.

On my Singular page for Genres, I have information about the Genre from my Genre CPT, then I want to show a list of related authors. There are too many authors to show all at once, so I want to paginate them.

This is where I hit my primary stumbling block with L&L. I can show an ACF Relationship loop, but it doesn’t paginate correctly. If it did, I would be done.

My workaround is to use an ACF Relationship Posts Module, which does paginate correctly. I can set a custom layout for that module. Inside that layout for authors I also want to show a list of his last three books. I should be able to use L&L for that, since I’m inside the Posts Module loop, but if I create an ACF Relationship loop inside the Posts Module loop, it doesn’t work.

Thus I am sending each author to an L&L template that creates a loop for that author and then an ACF Relationship loop for his books.

I get the sense that ACF Relationship loops have some unique properties, and limited capabilities.

The only thing I really need is to be able to paginate ACF Relationship loops.

Cheers, Richard

Ah ok, I think I understand a bit better now. I saw your post about pagination and I was stumped by it but I’m sure Eliot will drop into that thread soon to provide some clarification about why pagination isn’t working in that case.

One thing to clarify, when you say this:

Where exactly are you writing that L&L ACF Relationship loop? Do you mean that you’re writing it in the Custom Post Layout code area within your Beaver Builder Posts Module like in the screenshot below?

If that’s the case, then it makes sense that it wouldn’t work because Beaver Builder wouldn’t know what to do with L&L markup written in one of its HTML areas like the one shown above. L&L can only be written within a template. As noted in this article:

It’s important to keep in mind that while L&L markup looks similar to HTML, it works more similarly to dynamic languages in that it needs to be rendered before it can be displayed to your site’s visitors. […] Neither WordPress nor browsers natively understand how to display the dynamic tags in L&L markup, so it needs to be processed first inside of an L&L template.

Maybe that’s not what’s going on in your case, I just thought I’d clarify that just in case.

I probably misspoke about what I should or should not be able to do.

You’re absolutely right about markup differences. FWIW, I’ve noticed when converting from one to the other the easiest way to make sure you didn’t miss anything is to search for square brackets that are used in BB markup or search for curly brackets that are used in L&L markup.

For my workaround, my custom post layout contains one line that gets the job done, albeit loopy:
[template id=53567 pid=[wpbb post:id]]

The ultimate solution requires being able to paginate ACF Relationship loops.

You probably got this notification already on your other post, but it looks like pagination now works on ACF relationship loops :smiley:

1 Like