Views packs a powerful query filter that lets you search posts based on the date they were published, or even the date they were modified.
To add a Post Date filter:
- Select the main View block.
- In the right sidebar, expand the Content Selection section.
- Click on the Add a filter button, and select the Post date filter.
You can filter posts by comparing the published or modified date against one or more values.
The default state is to filter posts by their date compared to one value. You can use the options to check whether the post date is equal to, different from, before (or equal) to or later (or equal) to a given date. You will be presented with a series of options to set the date to check against, including date, month and year.
Note that you have many options. You can set the usual year, month, day, hour, minute and second. But you can also set the day of the week or the day of the month. Watch out and do not try to get posts published on a Tuesday, April 13, 2015 because it was actually a Monday. 🙂
You can also filter posts by comparing their dates against a set of values. Simply select to filter by dates in, not in, between or not between a set, and select the kind of date field to compare. For example, you can get posts published in 2012, 2013 and 2014:
Or you could get posts published in any day of the week but Saturday and Sunday:
You can set more than one condition for this date query filter. At the bottom, you can set the logical relationship between conditions: AND (which will return posts meeting all conditions) or OR (which will return posts meeting at least one of the conditions).
For example, you could get posts that were published before Jan 1, 2015 and were published between 9:00am and 12:00 am and were also published on a weekday from Monday to Friday.
When filtering by a date value, you can set a fixed date, use variable values, or pass the value as a parameter to the View.
Variable values cover mainly three options: CURRENT_ONE, FUTURE_ONE and PAST_ONE.
CURRENT_ONE sets the value on the moment that the query is run, and is quite useful to get posts published this same year, month, or even day of the week.
FUTURE_ONE and PAST_ONE need a reach number. They can be used to get posts published last month, for example.
Example 1: If a PAST_ONE value is used for the Year field and the numerical value is set to 2 , then the View will display posts published two years ago (relative to the current year).
Example 2: If a FUTURE_ONE value is used for the Month field and the numerical value is set to 1, then we are filtering for posts published in the month after the current month, regardless of the year published.You can also set the values to come from external parameters, using a shortcode attribute or a URL parameter. Using a shortcode attribute will let you reuse the same View for several different filters. You can filter by year set by a shortcode attribute filteryear, as shown in the following image.
Then, you can load your View like this:
[wpv-view name="My View" filteryear="2014"]
Here are the valid values for each date filter field:
- Year: must have a four-digit format
- Month: 1 to 12
- Day: 1 to 31
- Hour: 0 to 23
- Minute and second: 0 to 59
- Week: 1 to 53
- Day of the year: 1 to 366
- Day of the week: 1 to 7. This also depends on your WordPress settings. If you have set the week to start on Monday, then Monday will become 1 while Sunday will be 7. Otherwise, Sunday equals 1 while Saturday equals 7.
The Types plugin allows you to add custom fields for date and time. Filtering your Views using these date custom fields is a bit different than filtering by the Post Date.
The Types custom fields for date and time are stored in the database as UNIX timestamps, which are positive integers. Therefore, when inserting a query filter to test a date custom field you should use the NUMBER option.
The following image displays an example where a filter is used to only show posts where the date determined by the custom field, Deadline, is today.
You can enter a timestamp directly, or use the following functions to generate one:
- 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 previous 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) (N 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)
For example, 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.
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).
The _FROM_NOW() functions accept positive or negative numbers as parameters so that you can calculate future or past dates.
- Filtering Custom Lists of Posts
- Enabling Legacy Version of Toolset Views
- Styling Custom Searches
- Styling Views and Archives
- Adding Custom CSS to Templates, Archives and Views
- Passing Arguments to Views
- Display Dynamic Sources Inside all Text Blocks
- Displaying Form Data
- Displaying a List of Posts on A Map
- Creating Sliders with Dynamic Post Content
- Adding Dynamic Content to Your Homepage
- Creating a View
- Creating Responsive Columns and Grids
- Display Content Conditionally
- Using Third-Party Hidden Fields