Using loop to display and sort acf field by acf date

I am trying to sort some CPT’s by an ACF date field, but not having any luck.

This is what I currently have:

Upcoming Black Out Dates:<br>
<ul>
<Loop type=black-out-dates sort_field="bo_date" sort_type="date" sort_date_format="Y-m-d" sort_order="asc" >
  <li><If acf_date=bo_date after=today>
    <Field acf_date=bo_date format="F j, Y" locale=us />
    <Field acf_editor=reason />
    <Else />None!</If></li>
</Loop>
</ul>

My ACF date field is storing the date as: Y-m-d

Hi Shae! This comes up a lot but my number one recommendation when learning to work with L&L is to never invent attributes. Just like HTML, there are specific attributes that work with each tag in L&L, so it’s important to learn how the tag works and only use the attributes listed in the docs. Just like in HTML, made-up attributes will just be ignored.

If you haven’t already, you’ll probably want to read about the different methods for filtering a loop since that’ll give you some good examples and explain in which cases you might want to use query parameters, attributes, or a nested If tag as you’ve done here.

Once you’ve understood those three approaches, I’d first try using some query parameters to filter your loop. Probably give custom_date_field, custom_date_field_format, and custom_date_field_compare a shot to see if those work for you. As a value of the custom_date_field_value the docs mention that you could just write ="current" but I figured I’d mention that if ever you want to dynamically generate a date anywhere in L&L, you could also check out the Date tag, which might look like this instead: custom_date_field_value="{Date format='Y-m-d'}". Not really necessary for that particular attribute but it might be useful to know in case you ever need to dynamically generate the current date.

If that doesn’t work (it’ll depend on the format of your ACF fields), then you could try using some more flexible Loop tag attributes like field along with field_type, field_compare, and field_value.

I hope that gets you headed in the right direction!

Thanks for the help! It was pretty simple. I just need to add orderby_field=bo_date

<Loop type=black-out-dates orderby_field=bo_date >
  <li><If acf_date=bo_date after=today>
    <Field acf_date=bo_date format="F j, Y" locale=us />
    <Field acf_editor=reason />
    <Else />None!</If></li>
</Loop>

I got so caught up by your If tag that I missed the whole point of your thread hahaha. Glad you got this working with orderby_field!

That being said, I’m pretty sure your template isn’t working the way you intended. When I test out that syntax on my end, the logic renders false no matter what:

Where in the documentation did you find that the attributes acf_date and after work on the If tag? And that today is an accepted value of the after attribute? It’s totally possible that I’ve just missed it in the documentation, but it seems like made-up syntax that wouldn’t actually do anything.