Loop switch when only working for admin user

Hi,
I have an issue with Loop switch which used to work before.
Video explainer: Screen Recording 1-12-2023 at 15.31.webm

The Switch and When rule is only being applied when current user is Admin. I am not sure why or when this started to happen.

template:

<Set local=bp_fn_user><Route part=2 /></Set>
<If loop exists type=forestmessages custom_field=fm_forest_id custom_field_value="{Get local=bp_fn_user}" orderby=date order=desc paged=5>
<Loop>
  <div class="forestmessages-img">
    <Field acf_image=fm_planter_image width="50" height="50"/></div>
  <div class="forestmessages">
    <div class="fm_small">
      <Switch field="author_id">
        <When value="4454" />
        <Field acf_editor=field_5dd01c52cd659 /> <Field acf_editor=field_5dd01c6224c8c />  - <Field publish_date date_format="d.m.Y" />
        <When />
      <a href="https://forestnation.com/net/forests/{Field author_user_nicename}"><Field author_full_name /></a> - <Field publish_date date_format="d.m.Y" /><br>
      </Switch>
    </div>
    <div class="forestmessages-content"><p><Field content /></p></div>
  <div class="fm_amount"><span><Field acf_editor=fm_trees_planted /> Trees<br>
    </span></div></div>
</Loop>
  <Else />
      <div class="forestmessages-img">
    <img src="https://forestnation.com/net/wp-content/uploads/sites/7/2020/02/morio-green-100.png"></div>
  <div class="forestmessages">
      <div class="forestmessages-content">Be the first to plant for M.E!</div>
    </div>
</If>
<div class="forestmessages-pagination">
  <PaginateButtons /></div>

Hi Andrew, thanks for the video recording explaining the issue. Your template seems simple enough, but I’ve tried a few things on my end with the latest version of L&L and I don’t seem to be able to replicate your issue. I think in order to figure out what part of this isn’t working for you, we’re going to have to break this down into its components and see at what point it seems to be breaking.

I’ve tested this on my end with a really simple post loop and a Switch/When that’s checking the author ID as you’ve written. Swapping out my post type for yours, that’d look something like this:

<ul>
  <Loop type=forestmessages>
    <li>
      <Switch field="author_id">
        <When value="4454" />
        The post "<Field title />" is written by an anonymous user.
        <When />
        The post "<Field title />" is written by <Field author_name />.
      </Switch>
    </li>
  </Loop>
</ul>

If you put this on its own on a test page of your site, do you still have the issue where posts by that anonymous user ID aren’t being displayed when you’re not logged in as an admin?

Some other things to check/try:

  • Does your template work when you’re logged in with another user permission level (like subscriber or editor)?
  • Under your first When tag (where you currently have <Field acf_editor=field_5dd0...), are you able to display plain text instead of a field?
  • If so, are you able to display regular fields in that area (like <Field author_full_name /> or something) from the current post instead of ACF fields?
  • If the above ideas don’t reveal where the issue lies and how I could replicate it, are you able to try disabling all other plugins on your site other than the necessary ones (L&L+ACF probably) to see if the problem persists?

Let me know what you find during your troubleshooting!

Thanks for helping with this @benjamin, awesome! :slight_smile:
I’ll troubleshoot and get back to you soon.

Hi Andrew, I can’t tell from your screen recording if you’re using Elementor, but another user reported an issue similar to yours this morning, so I figured you might want to look into this to see if your issue might be caused by something similar:

Hi @benjamin

  • this contains your text template.

When not logged in I get the same issue and cannot see “is written by an anonymous user”
When logged in I can see “is written by an anonymous user”

If you do not have any other ideas I will test disabling plugins on our staging site.

thanks for helping and sorry for the late reply.

I am not using elementor. I am using Zion Builder.

Taking a look at your site, I’m seeing a bunch of posts that look like this:

The post “afrankland” is written by .
The post “Alicia Radov” is written by .

Are those the posts that are supposed to say “is written by an anonymous user”? If so, it seems like <When value="4454" /> is being evaluated as false when you’re not logged in. I wonder what L&L thinks that author ID field contains (if anything) when the user is logged out. Might be interesting to swap out <Field author_name /> in the template I wrote with <Field author_id /> and see if it displays a value of 4454 or something else. Another thing you could test to see if this issue is with Switch/When or if it’s an issue with the underlying data structure of the site (or L&L’s visibility into that data structure) would be to try filtering your loop to only display posts by that anonymous author. Something like this:

<Loop type=forestmessages author=4454>
  <p>The post "<Field title />" is written by an anonymous user with a user ID of "<Field author_id />".</p>
</Loop>

If that loop doesn’t display anything, then this is an issue with the field somehow not being visible to logged out users and isn’t specifically an issue with the Switch/When tags.

Out of curiosity, is this a multisite?

It’d probably be a good idea to check if this is a conflict with a third-party plugin just to know for sure.

If none of that leads to any insights that would help me replicate this on my end, then it might be helpful if you could send me some admin credentials to your site with this form along with some links to the pages you’re testing this on so that I could try to troubleshoot myself. If it’s a staging site that’d be ideal to avoid causing any issues for you on a production site.

Hi @benjamin I am really sorry for the late reply on this. The latest update of loops and logics seems to have solved this issue. (actually it could have been updates to another plugin also…) Thanks so much for helping me out though and sorry again for the late reply.

1 Like

Hi… this issue is back in version 3.1.8. Rolling back to 3.1.7 fixes it again. @benjamin

Hi Andrew,
Sorry to hear this cropped up again! I’m unable to replicate your issue. Is there anything non-standard about the “anonymous” user that I could try to replicate? Otherwise we may need access to your site to help debug, as Ben suggested in January.

Unrelated, but I’m noticing a little visual bug in the listing on Firefox, which you should be able to fix by applying display: inline-block; to the span:

forest-tree-count-glitch