Skip Navigation

[Resolved] Filter or search between 2 dates (minimum and maximum)

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to build a custom search by a date minimum and maximum values?

Solution:
https://toolset.com/forums/topic/search-a-date-between-start-date-and-end-date/#post-823107

This support ticket is created 6 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by christopheM 6 years, 7 months ago.

Assisted by: Beda.

Author
Posts
#814504

Hello,

I'm trying to set up an event website where visitors can pick a date and view every events available at this date.

I have no issue to set an event date and then search for that exact date, but is there any way to search between 2 dates (start and end) ?

Example with 2 events :
- Event 1 (one time event) : start date 15/05/2018 / end date 15/05/2018 (or no end date)
- Event 2 (6 days event) : start date 12/05/2018 / end date 18/05/2018

Now if I pick the date 15/05/2018 in my search, i want both events to show.
If i pick 17/05/2018, i only want Event 2.

Is this possible via Toolset ?

Thanks !

#823107

Yes, below are the steps to add a Custom Frontend Search by a Date Field, where you filter between 2 dates.

1. Create a View and query the post type where you use the Date Field (probably, "events", in your case)
2. Click on the "New Filter" button in the "Search and Pagination" section of the View, where you choose the Date Custom Field.
3. In the "Display Options" that appear as a pop-up, choose to "Using this comparison" > between.
4. Insert the ShortCode

This will create a Front-end Filter where the user can choose a minimum and maximum date from a date picker and submitting that search will show only posts with dates saved between the values set in the search.

Here you can read more about Date Queries themselves:
https://toolset.com/documentation/user-guides/date-filters/
https://toolset.com/documentation/user-guides/filtering-views-query-by-date/

Related to the issue you mention, let me reply below.

Example with 2 events :
- Event 1 (one time event) : start date 15/05/2018 / end date 15/05/2018 (or no end date)
- Event 2 (6 days event) : start date 12/05/2018 / end date 18/05/2018

Now if I pick the date 15/05/2018 in my search, i want both events to show.
If i pick 17/05/2018, i only want Event 2.

- If you pick the date 15/05/2018, then this is either a minimum or maximum date in a search as I show it above.
What is it in your case?

You need to set both dates in the search, to get a result of "between" two dates.

Searching by one specific date in a "range" of dates stored between multiple Date fields of the same post is possible, but you need to match the exact date in this case.

#823787

Thank you Beda.

If i understand correctly : if i want to search for a date in a range, i need to specify each date in that range ?

In that case set a single custom field 'date', make it able to be multiple and set each date ?

In my example above: if i want to be able to see Event 2 when i select 15/05, i need to set each date : 12/05, 13/05, 14/05, 15/05, 16/05, 17/05, 18/05

#823907

Well yes, if you want to be able to pick out ONE event by ONE date but the event may have been longer than the single day, then yes, there is no other way as storing and addressing this as a single field or a repeating instance of a field, although I disrecommend to use repeating fields just for ease.

I think you need a Custom Filter that allows to set 2 dates:
- start and end

Then when you submit the filter will return posts with ONE single date within that date range.

There is no need for repeating fields to find that one post.
Of course, you probably have a Field "Start Date" and "End Date", so you can easily just create 2 Custom Date Filters both filtering a range of those Date Fields.

Yes, the user will need to input practically 4 dates, in this case.
But it will return the expected results

#825078

Sadly i really need users to select a single date so i'll go with the repeating field solution.

It would be great if this is possible in the future. 😉

Thanks for your help Beda.

Have a nice day.