date_query in WP_Query: filter posts by date range, weekday and modified date
WP_Query has a date_query parameter that goes far beyond the simple year and monthnum arguments. It takes a list of date conditions joined with AND or OR, can compare single parts of a date (year, month, week, day, hour) or ranges (after and before), and can work on the publish date or the modified date. Typical uses: posts of this week, an archive for a date range, “updated in the last 30 days”, or scheduled posts in an editorial workflow.
Problem: Get posts by date conditions: published in the first quarter of the year, modified in the last 30 days, published on Mondays during the last six months, or in a fixed date range.
Solution: Describe each condition in date_query with the right column, the date parts or after/before, and the inclusive flag for the boundaries.
Examples
<?php
$year = (int) current_time( 'Y' ); // the year in the site's time zone
// 1. Published in the first quarter of this year
$q1 = new WP_Query( array(
'post_type' => 'post',
'date_query' => array(
array(
'year' => $year,
'month' => array( 1, 2, 3 ),
'compare' => 'IN',
),
),
) );
// 2. Modified in the last 30 days
$recently_updated = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'modified',
'date_query' => array(
array(
'column' => 'post_modified', // post_date by default
'after' => '30 days ago', // any string strtotime() understands
),
),
) );
// 3. Published on a Monday during the last six months
$mondays = new WP_Query( array(
'post_type' => 'post',
'date_query' => array(
'relation' => 'AND',
array( 'after' => '6 months ago' ),
array( 'dayofweek' => 2 ), // 1 = Sunday, 2 = Monday ... 7 = Saturday
),
) );
// 4. October 2021, both boundary days included
$october = new WP_Query( array(
'post_type' => 'post',
'date_query' => array(
array(
'after' => array( 'year' => 2021, 'month' => 10, 'day' => 1 ),
'before' => array( 'year' => 2021, 'month' => 10, 'day' => 31 ),
'inclusive' => true,
),
),
) );
// 5. January OR July of any year
$jan_or_jul = new WP_Query( array(
'post_type' => 'post',
'date_query' => array(
'relation' => 'OR',
array( 'month' => 1 ),
array( 'month' => 7 ),
),
) );
The available keys
- date parts:
year,month,week,day,dayofyear,hour,minute,second; - weekdays:
dayofweek(1 = Sunday … 7 = Saturday, as MySQLDAYOFWEEK()) anddayofweek_iso(1 = Monday … 7 = Sunday); - ranges:
afterandbefore, as astrtotime()string or an array withyear,month,day; comparefor the date parts:=,!=,>,>=,<,<=,IN,NOT IN,BETWEEN,NOT BETWEEN;column:post_date(default),post_date_gmt,post_modified,post_modified_gmt.
Boundaries
inclusive is false by default, and with the array format it works on whole days: 'after' => array( 'year' => 2021, 'month' => 10, 'day' => 1 ) starts after the end of October 1, and 'before' with October 31 ends before October 31 starts. With 'inclusive' => true both days are included, as in example 4.
A string like '2021-10-01' is turned into a timestamp, midnight of that day, so after with it already includes October 1 (from 00:00:01). If you need whole days, the array format is clearer.
NOTE: post_date is stored in the time zone from Settings → General, and post_date_gmt in UTC. Relative strings like '30 days ago' are calculated from the current time, so for exact boundaries pass the dates explicitly. The week key follows the “Week Starts On” setting of the site.