I want to create a view with a single exposed date filter, and have it select only nodes where that date falls between the from and to dates on a CCK Date field.
For instance, I have a content type called Schedule, with a single date field (field_dates) that has both "From" and "To" values. Let's say I have the following two nodes:
Schedule-1: nid=1; field_dates_value=2010-10-01; field_dates_value2=2010-11-30
Schedule-2: nid=2; field_dates_value=2010-10-22; field_dates_value2=2010-10-23
I want to create a view with one exposed date filter, called "Date". If Date=2010-10-06, then the view should return Schedule-1. If Date=2010-10-22, then the view should return Schedule-1 and Schedule-2. If Date=2010-12-02, then the view should be empty.
I can set up two separate filters—one filtering on Date>=field_dates_value and one filtering on Date<=field_dates_value2—but that means I have two separate exposed filters and I only want people to enter a single date.
Comments
Comment #1
jstollerMy current workaround is to set a single exposed filter on the "From" date, then alter the database query using a views hook to add my "To" date filter. The code in my module is as follows:
This appears to work, but it seems like there should be an easier way to do this from within the Views GUI.
Comment #2
capellicThanks for your solution. For those following along out there, defined your field as only being the "from" date and then set it to "is less than or equal to". The above code is looking for a pattern based on this and then adding the "to" date where it must be greater than the filter date. Of course you'll have to also update the code to match you field name.
Thanks, @jstoller!
Comment #3
13rac1 CreditAttribution: 13rac1 commentedI've made this functionality into a contrib module for D7: https://drupal.org/project/views_between_dates_filter
Closing this old support request.
Comment #4
jstollerI'm re opening this as a feature request. I'm glad to know that there is a module out there now, but this really should be a standard feature and not require installation if another module, IMHO.
Comment #5
bdanin CreditAttribution: bdanin commentedDoesn't this already exist in Views 3.x ?
I recently had a project where I needed this functionality, and was able to use date as an exposed filter with the operator "is between"
Other than some odd conflicts with date-picker (pop-up), this worked as expected. Another note, it's best to use date fields as opposed to published dates, due to date having more ability to choose granularity in your sorting.
Note: this requires using the date_views module (part of date), but it still works and is relatively easy to setup.
Comment #7
casper83 CreditAttribution: casper83 commentedI am having problem after saving the filter to raise a handler error instead of the field !>