Acf_date and loop count

Hi, I’m am trying to show 3 events filtered by an acf_date field, showing only future events.

This works:

<Loop type=event orderby_field=event_date order=asc>
  <If acf_date=event_date after=today>
    <Field title />
  </If>
</Loop>

But when I try to limit it to 3 posts, eg:

<Loop type=event count=3 orderby_field=event_date order=asc>
  <If acf_date=event_date after=today>
    <Field title />
  </If>
</Loop>

It shows less than 3 because the past events are still being counted but not shown.

I’m guessing it should be something like this but am not sure of the exact syntax:

<Loop type=event count=3 orderby_field=event_date order=asc custom_date_field=event_date custom_date_field_format="d/m/Y" custom_date_field_compare=before custom_date_field_value=current>
  <Field title />
</Loop>

But nothing shows up for that. Any suggestions?

Hey Paul, good thinking, you’re on the right track and I think the only reason your last approach doesn’t work may be because of a bug.

The reason that the first approach doesn’t work is that your loop is getting three items before the If tag runs. So it gets whatever the first three posts are that match your query, and then the If tag filters it to only show results that also match your logic. So if a post matches the query parameters but not the logic, it’ll get counted toward the count=3 but then won’t be displayed because it doesn’t fit the logic in the If statement. That’s why you need to put the logic/filter/query parameters inside your loop tag so that it knows how to filter the results of the query while the query itself is happening. You can check out the filtering the loop section of the docs for a bit more info on how to think about that.

Regarding your issue with nothing showing up, I just tested the template you shared and I was able to confirm that it doesn’t seem to be working with custom_date_field_value=current which is what’s written in the docs. I did some digging and found this post from Eliot and it inspired me to try with custom_date_field_value="{Date format=Ymd}" which seems to work correctly. In that post, Eliot mentions using custom_date_field_value=today and interestingly enough, that seems to work correctly as well. So I wonder if there was some update where current was replaced with today. I imagine in an ideal world, both would work since current is pretty commonly used elsewhere in L&L.

I hope this discovery gets your template working correctly and I’ll report this so that this query parameter is made to work with current again in a future update.

1 Like

Perfect, thanks! I can confirm custom_date_field_value="{Date format=Ymd}" and custom_date_field_value=today work as expected.

1 Like