How to Filter Posts by Date Range
Not so long ago I was adding a posts filter by date range into my site’s admin dashboard and I thought why not share the whole process of doing it here, on my blog?
It is useful to mention that I will show you how to add a filter by date range not only for regular WordPress posts and pages but also for any custom post type and even attachments (media library).
In the first part of the tutorial, we will dive into the examples of using the date_query parameter of the WP_Query class. The second part of the tutorial will be more practical because we’re going to create an actual date range filter for both posts and the media library page:

date_query parameter of WP_Query
In case you’re creating your custom filter functionality or something and you were just wondering how to make it work under the hood, let’s start with the basics first – let’s talk about the date_query parameter of the WP_Query class.
This parameter appeared a long time ago, actually, in WordPress 3.7 and it is as convenient and sick as the meta_query parameter.
Let’s jump straight to the examples then!
Example 1. Between days of the week
We can easily get posts or attachments that were published between specific days of the week using the dayofweek array key.
For example, we can get all posts published from Monday to Thursday using this snippet:
$args = array(
'date_query' => array(
array(
'dayofweek' => array( 2, 5 ), // 2 – Monday, 5 – Thursday
'compare' => 'BETWEEN',
),
),
);
// create a custom posts loop
$query = new WP_Query( $args );If you decide to get posts published only on weekends (Saturday and Sunday), it is not necessary to provide the compare parameter with the BETWEEN value, because 7 (Saturday) and 1 (Sunday) are in a consecutive order anyway.
Since we don’t have so many days in a week, nothing stops you from providing all the day numbers like this: 'dayofweek' => array( 2, 3, 4, 5 ) and to completely skip the compare parameter.
By the way, you can use the dayofweek_iso instead of the dayofweek if you prefer to start the week on Monday.
Example 2. Using complex conditions
The same way we created complex conditions for the meta_query we can do it for the date_query as well – using multiple sub-arrays and the relations parameter.
For example, let’s try to filter our posts both by the days of the week and by a timeframe period of the day.
$args = array(
'date_query' => array(
// timeframe period
array(
// either an early morning
array(
'hour' => 7,
'compare' => '<=',
),
// or an evening
array(
'hour' => 17, // after 5PM
'compare' => '>=',
),
'relation' => 'OR',
),
// days of the week
array(
'dayofweek' => array( 1, 5 ),
'compare' => 'BETWEEN',
),
'relation' => 'AND',
),
);
$query = new WP_Query( $args );The relation parameter has AND value by default, you skip it in the above example and add it only if you need to use OR value.
Example 3. Get posts between two dates
Ok, now we’re getting close to the actual goal of this article – to filter WordPress posts by a date range.
$args = array(
'date_query' => array(
array(
'after' => array(
'year' => 2017,
'month' => 1,
'day' => 8,
),
'before' => array(
'year' => 2018,
'month' => 4,
'day' => 20,
),
// in case you want to include the provided dates as well
'inclusive' => true,
)
)
);
$query = new WP_Query( $args );Both before and after parameters support different date formats, for example, below, when creating a filter by date range in the WordPress admin, I am using the YYYY-MM-DD format.
Example 4. Filter posts across all sites in WordPress Multisite by a date range
In case you’re using my plugin which allows you to get posts from all blogs across your WordPress multisite network with the help of the Network_Query class (similar to WP_Query), then I have good news for you, my plugin also supports the date_query parameter.
Basically, you can get posts between two dates with the following code snippet:
$args = array(
'date_query' => array(
array(
'after' => array(
'year' => 2025,
'month' => 2,
'day' => 5,
),
'before' => array(
'year' => 2025,
'month' => 19,
'day' => 31,
),
// in case you want to include the provided dates as well
'inclusive' => true,
)
)
);
$query = new Network_Query( $args );Filtering Posts by Date Range in the Admin Dashboard
It is time to create a nice date range filter with a date picker in the WordPress admin dashboard:

In my opinion, the only challenge here, is to design a nice and slick calendar with CSS, because the standard jQuery UI CSS is not what we need. Luckily, I have already created it for you, you can get it from this article.
Also, don’t forget to include thejquery-ui-datepicker script. By the way, you don’t need to do that if you’re using WooCommerce.
First things first, let’s add fields to the filter form. We can do it with the help of the restrict_manage_posts action hook:
<?php
add_action( 'restrict_manage_posts', function() {
$date_from = isset( $_GET[ 'rudr-date-from' ] ) && $_GET[ 'rudr-date-from' ] ? $_GET[ 'rudr-date-from' ] : '';
$date_to = isset( $_GET[ 'rudr-date-to' ] ) && $_GET[ 'rudr-date-to' ] ? $_GET[ 'rudr-date-to' ] : '';
?>
<input type="text" name="rudr-date-from" placeholder="Date From" value="<?php echo esc_attr( $date_from ) ?>" size="15" />
<input type="text" name="rudr-date-to" placeholder="Date To" value="<?php echo esc_attr( $date_to ) ?>" size="15" />
<?php
} );If you wish to remove the standard filter by month you can use this code line:
add_filter( 'months_dropdown_results', '__return_empty_array' );Adding date pickers to fields:
jQuery(function( $ ) {
const fromField = $( 'input[name="rudr-date-from"]' )
const toField = $( 'input[name="rudr-date-to"]' )
// by default, the dates look like "April 3, 2017"
// let's make them look like "2017-04-03" for convenience
const customDateFormat = 'yy-mm-dd'
// create datepickers
fromField.datepicker( { dateFormat : customDateFormat } )
toField.datepicker( { dateFormat : customDateFormat } )
// prevent a user from choosing an incorrect date interval
fromField.on( 'change', function() {
toField.datepicker( 'option', 'minDate', fromField.val() )
});
toField.on( 'change', function() {
fromField.datepicker( 'option', 'maxDate', toField.val() )
});
});For this jQuery code, I would recommend to create a separate .js file and include it with the jquery-ui-datepicker dependency.
And finally, filtering the results:
add_action( 'pre_get_posts', function() {
global $pagenow;
// pre_get_posts action hook is applied to all loops on a WordPress website
if( ! is_admin() ) {
return;
}
if( ! $query->is_main_query() ) {
return;
}
// edit.php – regular posts
// upload.php – media library
if( ! in_array( array( 'edit.php', 'upload.php' ) ) ) {
return;
}
// if both filter fields are empty, nothing to do here
if( empty( $_GET[ 'rudr-date-from' ] ) && empty( $_GET[ 'rudr-date-to' ] ) ) {
return;
}
$date_query = array(
'inclusive' => true, // include selected days as well
'column' => 'post_date',
);
if( isset( $_GET[ 'rudr-date-from' ] ) && $_GET[ 'rudr-date-from' ] ) {
$date_query[ 'after' ] = $_GET[ 'rudr-date-from' ];
}
if( isset( $_GET[ 'rudr-date-to' ] ) && $_GET[ 'rudr-date-to' ] ) {
$date_query[ 'before' ] = $_GET[ 'rudr-date-to' ];
}
$query->set( 'date_query', $date_query );
} );By the way, you can filter not only by the publish date but also by the last update date if you change the value of the column parameter to post_modified. It also supports post_date_gmt and post_modified_gmt values.
Misha Rudrastyh
Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Great,
You rock!
Thanks!
AWESOME!!! You saved my day, thanks!
Hi,
Thank you so much, but it’s not working in WordPress multisite.
Hi,
I added an example for WordPress Multisite.
Not working. Functions add filters in post view and library view, but when i choose date for example 6 june 2018 to 6 june 2018 i didn’t get anything. What’s wrong ? How can i fix this?
For issue @Fichtner faced above, here’s my solution:
I just re-read the document on WP_Query#Date_Parameters, the solution can be shorter:
I am facing the same problem with the previous user.
can you post a code if i only want to add “Today” filter option in all dates filter which wordpress already have
Great. Thanks for the effort!
Thanks! <3
This is great. Thank you!
Thank you so much this code and the phenomenal tutorial.
But this code will work in the Media Library, or Post and Pages.
How can I adopt it to be work in the Add Media modal pop-up?
That’s an interesting idea, thanks!
Doesn’t work… :( It was a GREAT idea/integration if it worked…
Hi Alessandro,
Everything works 🙃
Thank you so much!
Great sfuff, works perfect. Thanks
Thanks a lot for this. If you are querying meta data, that query can also be written as: