Views packs a very powerful query filter that lets you search posts based on the date they were published, or even the date they were modified.
In the View Query Filter section, click on the Add filter button. Then, select to filter by Post date and this dialog will be shown:
You can filter posts by comparing the date field selected (published or modified date) against one value or against multiple values.
Compare against a date
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. 🙂
Compare against a group of dates
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:
Combine conditions
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).
This means that 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.
Or you could get posts published between 2010 and 2014 that got modified along 2015.
You could even get posts that were published or modified the last month.
A word on values
When filtering by a date value, you can set a fixed date, use some of the built-in functions for 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.
The 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 we are filtering (displaying) 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 one month from now (relative to the current month).
You can also set the values to come from external parameters, using the two classical ways to pass information into a View: a shortcode attribute or an 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"]
When passing values into the View, or when using fixed values, you need to make sure to set the valid ones. When setting a year value, it must have a four digit format. Month values go from 1 to 12 while day values go from 1 to 31. Hour values go from 0 to 23 while minute and second values go from 0 to 59. Week values go from 1 to 53, and the day of the year covers from 1 to 366.
The day of the week value 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.