• Resolved polarracing

    (@polarracing)


    Hi,

    this code

    <List name=racing>
      <Item>item_1</Item>
      <Item>item_2</Item>
    </List>
    
    <Loop list=racing >
      <Set name="this_term"><Field /></Set>
      <Loop acf_repeater=page_content>
        <If field="content_name" value="{Get this_term}">
          <Field content_text />
        </If>
      </Loop>
    </Loop>

    does not work – until I enter the following code-block before I run the list loop.

    <Loop acf_repeater=page_content>
      <Set name="_trash"><Field content_name /> - <Field content_text /></Set>
    </Loop>
    <Set name="_trash"></Set>

    As you can see, the “_trash” is not used – but without my acf_repeater loop is empty – with the block it works.

    • This topic was modified 2 years, 6 months ago by polarracing.
    • This topic was modified 2 years, 6 months ago by polarracing.
Viewing 11 replies - 1 through 11 (of 11 total)
  • I’m not sure why the markup you’ve written doesn’t work, so a more experienced dev would need to chime in here to confirm what’s happening in your case.

    I wonder if you’d be able to achieve the same result by using the list variable as the value of a conditional statement using the in comparison. The only difference I could see with this approach in contrast with yours is that with my approach the order of the items being looped through would be set by the order of the ACF repeater fields as opposed to your markup where the order of the “racing” list is what sets the order. If that difference doesn’t matter to you, then maybe you could adapt something like this to work on your site:

    <List name=racing>
      <Item>item_1</Item>
      <Item>item_2</Item>
    </List>
    
    <Loop acf_repeater=page_content>
      <If field="content_name" in value="{Get list=racing}">
        <Field content_text />
      </If>
    </Loop>

    The nice thing about this markup is that it’s just a single loop and doesn’t involve setting a new variable for each instance of the outside loop as you’re doing in your markup. I just checked both methods using the Timer tag and the markup above is about twice as fast as your nested loop solution.

    Sorry that I can’t explain why your method only works when you add that dummy _trash loop/variable, but hopefully this alternative method works for you and results in better performance anyway.

    Thread Starter polarracing

    (@polarracing)

    Thanks for trying to help. I gave it a try and it did not work.

    I need to do that list loop anyway – as I need different values based on the actual list-item looped.

    It looks like the context is lost in a list loop as I can’t even access the fields on the current page in list loop without the _trash – stuff.
    If I run the acf_loop without any if clause – I’m not able to get the fields of the current page – instead I’m getting back field from pages called racing, etc.

    Hmm, that’s interesting. I was initially wondering if the issue you were describing was a result of the loop context changing once you’re inside a list loop, just as you hypothesized. But I tested that (I made an acf_relationship loop nested inside a list loop) and the inner acf_relationship loop was successfully able to get fields from the current page. You’re saying that on your site, that’s not happening and you’re instead getting fields from some other pages.

    I might be completely wrong on this, but I wonder if your variable named this_term happens to be conflicting with some other variable on your site. Maybe you could try changing that to something that’s more obviously unique.

    If that doesn’t change anything, I think the biggest clue as to why it’s working for me and not working for you is that, as you mentioned, you’re “getting back fields from pages called racing, etc.” Are you able to see what’s in common between your list items and the pages from which you’re accidentally getting field data? Is the issue here that when your list item name matches a page name, the acf_repeater loop is for some reason looping through the fields from that matched page instead of the current page?

    These might be dead ends, but I figured it’d be worth a shot since the fact that it works on my little test site but not your site suggests that the culprit might be some conflicting data on your site.

    I just noticed that in your reply you mentioned that my markup did not work on your site, which seems especially odd. What part of it didn’t work? My markup shouldn’t be affected by a change in loop context since it’s just a single loop with a conditional statement. If you use this part of my markup without the List tag or anything else, are you able to successfully output some of your acf_repeater fields?

    <Loop acf_repeater=page_content>
      <Field content_name />
      <Field content_text />
    </Loop>

    If not, then that seems like it’d simply be indicative of either incorrect ACF field names being referenced in the L&L markup or a pretty fundamental problem with the acf_repeater loop in your particular use-case.

    And one other thought: does your markup only work when you set and overwrite the _trash variable, or does simply having an empty <Loop acf_repeater=page_content></Loop> loop before the list loop also make your markup work? It seems strange that setting and wiping an unused variable would have any bearing on the rest of the markup so I’m trying to better understand in what contexts your markup works or doesn’t work.

    Thread Starter polarracing

    (@polarracing)

    I just double checked your approach and I realized that the list contains only a part of the total term used for the loop.

    That’s why it didn’t work. I would need to change my whole code to implenent your solution.

    Also I’m using it to generate static pages in different languages – so performance is more a question of my caching-plugin than of the loops. ??

    Ah ok, so the part that doesn’t work about my approach is the order of the If statement since your racing list term is in the content_name field, but not vice versa (the content_name field isn’t in the racing list term). Would reversing the order of my If statement work while also not requiring you to change your whole code? So something like this:

    <Loop acf_repeater=page_content>
      <If check="{Get list=racing}" in value="{Field content_name}">
        <Field content_text />
      </If>
    </Loop>

    If that still doesn’t work, let me know what you learn from testing the other three things I suggested, it might surface some valuable information about why your particular site/installation/data structure seems to be causing issues but mine (a very vanilla WP install) works as expected:

    1. Check whether changing the this_term variable name has any impact. (I imagine it won’t but it’s worth a shot)
    2. See what’s in common between your list items and the pages from which you’re accidentally getting field data. Is the issue here that when your list item name matches a page name, the acf_repeater loop is for some reason looping through the fields from that matched page instead of the current page?
    3. Check whether your markup only works when you set and overwrite the _trash variable you made or whether simply having an empty <Loop acf_repeater=page_content></Loop> loop before the list loop is enough to make your markup work.
    Thread Starter polarracing

    (@polarracing)

    And one other thought: does your markup only work when you set and overwrite the _trash variable, or does simply having an empty <Loop acf_repeater=page_content></Loop> loop before the list loop also make your markup work?

    Nope, just tested. If I do not call the fields – no content on the page.

    I just realized that my markup above wouldn’t work since it’s comparing the entire racing list against the current content_name field. Guess I didn’t think that one through all the way.

    If you end up testing the two other points I suggested in my earlier post, let me know what you discover from that! I’m still not able to replicate the issue, but maybe with a bit more information about what kind of data your post loop is getting “contaminated” with, @miyarakira might be able to make some suggestions.

    Thread Starter polarracing

    (@polarracing)

    Check whether changing the this_term variable name has any impact. (I imagine it won’t but it’s worth a shot)

    Does not change anything. this_term is set right before – so its accurate.

    See what’s in common between your list items and the pages from which you’re accidentally getting field data. Is the issue here that when your list item name matches a page name, the acf_repeater loop is for some reason looping through the fields from that matched page instead of the current page?

    The name of the pages I get content from is a part of the field name I try to get.

    Check whether your markup only works when you set and overwrite the _trash variable you made or whether simply having an empty <Loop acf_repeater=page_content></Loop> loop before the list loop is enough to make your markup work.

    Yes, I need to overwrite the variable, a simple loop does not work.

    • This reply was modified 2 years, 5 months ago by polarracing.
    • This reply was modified 2 years, 5 months ago by polarracing.
    Plugin Author Eliot Akira

    (@miyarakira)

    Hello,

    I may have figured out why the code is not working. So the original snippet:

    <List name=racing>
      <Item>item_1</Item>
      <Item>item_2</Item>
    </List>
    
    <Loop list=racing >
      <Set name="this_term"><Field /></Set>
      <Loop acf_repeater=page_content>
        <If field="content_name" value="{Get this_term}">
          <Field content_text />
        </If>
      </Loop>
    </Loop>

    The inner loop is trying to get the ACF repeater field “page_content” from the outer loop, which is the list “racing”. But you want to get the field from the loop outside of *that*, the default loop context which is the current post.

    Hmm, I’m trying to think of a solution, but it will be tricky..

    <List name=racing>
      <Item>item_1</Item>
      <Item>item_2</Item>
    </List>
    
    <Loop acf_repeater=page_content>
      <Set content_name><Field content_name /></Set>
      <Set content_text><Field content_text /></Set>
      <Loop list=racing>
        <If check="{Get content_name}" value="{Field}">
          <Get content_text />
        </If>
      </Loop>
    </Loop>

    It’s not particularly efficient, since the repeater loop needs to get both field values, even when the second one may not be needed. But, as far as I can think, this is the only way to get each repeater field’s child fields while inside a list loop.

    Thread Starter polarracing

    (@polarracing)

    Yes, this solution was also in my mind.

    As I said, I’m creating static pages in multiple languages, so runtime is not the key issue as the pages are fully cached anyway.

    So as long as the trashing solution works, I will go with that and do not need to change my code. ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘List Loop’ is closed to new replies.