Current Date Between Two Fields

We have a field for event_start and another for event_end and want to output a list of current events. Eg: those that are between event_start and event_end.

Using custom_date_field / custom_date_field_compare if works for either before or after, but it doesn’t look like these fields can be used with _* - eg: custom_date_field_2 / custom_date_field_compare_2

Is there any way around this, or something else I’m missing?

Hey @baldidiot Can you give me more details about the case you’re trying to solve? It would be helpful to know what plugins you’re using, too. This will help me recreate the case and give you better help. Thanks!

In terms of plug ins - loops & logic plus ACF for the fields and CPTUI for the CPT.

We have a CPT for events with fields for the start and end date of the event. We just want to show a list of events that are in progress. Eg: event_start_date < NOW < event_end_date

These event posts stay live and get output in other places outside of the event dates, so we can’t do it by scheduling them or expiring the posts.

We can do one or the other using L&L, for example:

custom_date_field=event_end_date custom_date_field_compare=after_inclusive custom_date_field_value=current

Essentially all we need to do is apply a second variable to the query to check if it has started, but we don’t seem to be able to use multiple instances of the custom_date_field (at least, not according to the docs and it hasn’t worked when I’ve tried).

I’m aware we could just use <If> on the output to achieve this result, but it wouldn’t be as efficient as just comparing both custom field values in the loop.

I just took a look at the docs and I noticed that custom_date_field_2 doesn’t exist, so that’s probably your issue!

It seems that while custom_date_field doesn’t allow for multiple filters at the moment, the basic custom_field parameter does. I’m not 100% sure why custom_date_field exists when you can also filter dates using custom_field, but I imagine it’s because custom_date_field_value allows you to just write a date value whereas custom_field_value would require you to potentially use the Date tag for that which is a bit more verbose.

In any case, I think you should be able to simply work with custom_field using something like this:

<Loop 
      post_type=post 
      custom_field=my_custom_date_field
      custom_field_type=datetime
      custom_field_compare=before_inclusive
      custom_field_value="{Date format='Y-m-d H:i:s'}today{/Date}"
      custom_field_2=my_custom_date_field
      custom_field_type_2=datetime
      custom_field_compare_2=after
      custom_field_value_2="{Date format='Y-m-d H:i:s'}September 1, 2023{/Date}">
  <p><Field title /></p>
</Loop>

I’ve tested this and it all works on my end, so you’ll just need to change the field names and timing to make it logical for your use case. I think that since the Y-m-d H:i:s format already orders things in “alphabetical” order, you probably don’t even need custom_field_type=datetime, but I figured I’d leave it in the example above for the sake of clarity. I think the raw values of ACF datetime fields are always in the Y-m-d H:i:s format.

1 Like

Thanks Benjamin, most appreciated - apologies for the slow reply, I didn’t see your reply until now!

2 Likes