ACF Relationship Loop orderby/sort using event date field

Hi,

I am trying display a list of related events using and ACF relationship field. I was to display a list of events that shows related events ordered by event date, more recent or upcoming at the top and older/past at the bottom. I was looking through documentation and found something like this, however it’s not ordering. So something isn’t right, any insights on that would be appreciated.

<Loop acf_relationship="related_events" sort_field="_eventorganiser_schedule_start_start" sort_type="date" sort_date_format="d-m-Y" sort_order="desc">
  <li><a href="{Field url}"><Field title /></a> - <Date format="l, F j, Y"><Field _eventorganiser_schedule_start_start /></Date> at <Date format="g:i a"><Field _eventorganiser_schedule_start_start /></Date></li>
</Loop>

Also if there’s a way I can show two lists, with future events and another of past events that would be great. As shown in documentation here.

<Field type=event custom_date_field=event_date_time custom_date_field_format="Y-m-d H:i:s" custom_date_field_compare=before custom_date_field_value=current

How would I incorporate that to the loop?

Thanks!

Hi @danaskallman! I just wrote up a whole response explaining that I’m able to use your exact template on my WordPress installation and it works fine, but then I noticed that I was running the previous version of L&L (2.4.4). I updated to the latest version (3.0.0) and then it stopped working. So the good news is that I was able to confirm that your markup is correct, but it seems like it simply isn’t working in the latest version of the plugin. I’ve passed my findings along to the dev team so I’m sure they’ll address this in an update.

In the second part of your post, I noticed that the example starts with Field type=event... which isn’t right. This was my mistake when I wrote up that example in the post loop documentation and it should have been Loop type=event.... As far as how you’d incorporate that into your loop, this may seem obvious now that I’ve clarified my mistake but it would just be a matter of adding those query parameters to your other Loop tag. I’m not able to test this at the moment because of the issue mentioned above, but I’ll keep this on my to-do list and follow up here with clarifications.

2 Likes

Hi @benjamin and @danaskallman !
I’m chiming in here because I think I’m running into a similar issue as I’m trying to sort using an acf_date_time parameter, and can’t seem to make it work. Is there any update on the issue? Is it supposed to be solved or is it still hanging?

Hi @Adrien_C, the great and powerful @eliot wasn’t able to address this in time for the 3.0.1 release but it’s being investigated and I’m sure it’ll be addressed in an upcoming update. I’ll be sure to post here when the fix gets scheduled for release.

1 Like

Hi @danaskallman, I initially thought this thread might be related to some bug with sort_field but I was taking another look at this “bug” report the other day and I noticed something I hadn’t before: the query parameters you’re using to filter your loop here are query parameters from the post loop, but those same query parameters aren’t officially supported on an ACF relationship loop. I think that might be your issue here. Maybe in a previous version of the plugin, the ACF relationship loop supported filtering using query parameters from the post loop, but this doesn’t seem to have ever been documented and may not actually have been intended anyway.

If you want to safely use the query parameters from the post loop on the posts listed in your ACF relationship field, you might want to “convert” your ACF relationship field into a post loop which I assume should allow you to use all those query parameters from the post loop. You can let me know if this works for you. The way I’d do this would be to feed your ACF relationship field to the id parameter of a post loop (or in your case, an event CPT post loop) like this:

<Loop type="event" id="{Field related_events}" sort_field="_eventorganiser_schedule_start_start" sort_type="date" sort_date_format="d-m-Y" sort_order="desc">
  <li><a href="{Field url}"><Field title /></a> - <Date format="l, F j, Y"><Field _eventorganiser_schedule_start_start /></Date> at <Date format="g:i a"><Field _eventorganiser_schedule_start_start /></Date></li>
</Loop>

Does that work for you?

1 Like

I see @benjamin so the id field limits the post listing to display those that are set in the ACF Relationship field. Yeah, that works. Thanks!

Exactly, ACF relationship fields just contain an array of post IDs which you can see if you try to just display the raw contents of the field using <Field related_events />. And if you wanted to manually limit your post loop to only specific post IDs, you could write <Loop type=post id="11,12,56,95"> for example. In the example I showed, you’re just passing the post ID array that exists in your ACF relationship field to the id parameter which accepts arrays of posts.

Glad that works for you!