Skip Navigation

[Resolved] Bug when filtering between 2 dates

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

Problem:
Posts have start and end dates.

Client wants a custom search View to be able to specify start and end dates as a between filter so that if there is any overlap between the range of start-date to end-date of the posts and the filtered date range then posts should match and be displayed.

Solution:
You need to compare the end date of your filter with the start date of your post and the start date of your filter with the end date of the post.

So a partial overlap of the posts and the filters being applied would be achieved if

- the post end date is later than the filter start date (the post hasn't already finished when our filter period starts), and
- the post start date is earlier than the filter end date (the post doesn't begin after our filter period is over)

Insert the start and end filters as normal, then edit them to swap over the URL parameters that provide the filter value.

This support ticket is created 5 years, 5 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.

Our next available supporter will start replying to tickets in about 0.34 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by DavidW2160 5 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#1264733
Map 1.png
Map 2.png

I am trying to: I have a custom post type with a "Start Date" and "End Date". I'm using a search filter for both start and end dates.

Here's the query filter:
Select items with field:
Start Date is a number between URL_PARAM(wpv-wpcf-start-date), URL_PARAM(wpv-wpcf-end-date)
OR
End Date is a number between URL_PARAM(wpv-wpcf-start-date), URL_PARAM(wpv-wpcf-end-date)

Here's the Search screen

<label>[wpml-string context="wpv-views"]Sale Start Date[/wpml-string]</label>
[wpv-control-postmeta field="wpcf-start-date" type="date" default_date="TODAY()" url_param="wpv-wpcf-start-date"]
<label>[wpml-string context="wpv-views"]End Date[/wpml-string]</label>
[wpv-control-postmeta field="wpcf-end-date" type="date" url_param="wpv-wpcf-end-date"]

Link to a page where the issue can be seen: You have access from an earlier post to hidden link

I expected to see: When I search on a date that is actually between the Start and End date (e.g. I set Sale Start Date and End Dates to Jun 22, 2019), I see events that start or end on that date, but there's an event that spans from Jun 21 to Jun 23, and this event doesn't show up on the results screen.

The images show two events, both span Jun 21 -Jun 23. When I search on Jun 22 start and no end date, it shows both. When I search on Jun 22 as both start and end date, it shows neither.

#1265073

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi David

Date fields are timestamps, i.e. the number of seconds since 1970, and are a point in time.

So if you filter start and end dates with the same "date", e.g. 22 June 2019, then you have both start and end "dates" as 2019/06/22 00:00, so an event with start date 21 Jun and end date 22 Jun will fail such a filter.

You actually need to flip this around and compare the end date of your filter with the start date of your post and the start date of your filter with the end date of the post.

So a partial overlap of the posts and the filters being applied would be achieved if

- the post end date is later than the filter start date (the post hasn't already finished when our filter period starts), and
- the post start date is earlier than the filter end date (the post doesn't begin after our filter period is over)

So start over with your conditions, inserting controls for later than start date and before end date (with AND not OR), and then manually edit where the shortcodes are inserted to swap over the URL parameters, e.g. the wpv-wpcf-start-date and wpv-wpcf-end-date.

#1271125

My issue is resolved now. Thank you!