Date filters let you select specific posts, according to time and date. You can build filters for specific dates, or relative to the current time.
If you have a Custom Post Type with a Custom Field of a Date type, you can create a precisely filtered view that populates data for a given time interval. This could be used for calendar projects, displaying orders or invoices for a specific period, listing all users registered for the past year and more.
In order to use the Date filters, you should select a Date custom field and apply a comparison operator based on a specific value:
You can select the Date custom field you want to filter by, the comparison function (less than, greater than, less than or equals etc) and the value to compare with, which is a function from our list below.
Don’t forget to select the ‘Numeric’ value from the comparison list as dates in Types are stored as timestamps and that’s the way we compare them.
We have several predefined functions to be used for dynamic calculation of a date in your project:
- NOW() (the time at the moment in milliseconds)
- TODAY() (time at 00:00 on today)
- FUTURE_DAY(int) (time at 00:00 on the following day)
- PAST_DAY(int) (time at 00:00 on the following day)
- THIS_MONTH() (time at 00:00 on first day of this month)
- FUTURE_MONTH(int) (time at 00:00 on first day of future month)
- PAST_MONTH(int) (time at 00:00 on first day of past month)
- THIS_YEAR() (time at 00:00 on Jan 1st of this year)
- FUTURE_YEAR(int) (time at 00:00 on Jan 1st of a future year)
- PAST_YEAR(int) (time at 00:00 on Jan 1st of a past year)
- SECONDS_FROM_NOW(int) (1000 seconds from the moment of execution)
- MONTHS_FROM_NOW(int) (N months from the moment of execution, day of month is persisted)
- YEARS_FROM_NOW(int) (N years from the moment of execution, day and month are the same)
- DATE(dd,mm,yyyy) eg. DATE(21,10,2011)
Let’s say that the current date is 5th of May, 2012.
- PAST_MONTH(2) will return 1st of March, 2012.
- FUTURE_DAY(10) will be 15th of May, 2012
- YEARS_FROM_NOW(-5) will result in 5th of May, 2007.
Notice that some of the functions return the first day or month of a year, while other functions keep the day/month value (you might need both depending on the project). Also, the _FROM_NOW(…) functions take positive or negative numbers as parameters so that you can calculate future or past dates.
Filtering Between Dates
Another handy use of the date filters is via the BETWEEN function. You can list all entries between two dates. Just select the BETWEEN function from the dropdown and list the two date functions below, separated with comma. This would list all entries with a date custom field value between the first and the second function, for instance listing all records for the current month: