Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add a Date Range Filter to WooCommerce Admin Orders Page (HPOS Compatible)?

Managing orders in WooCommerce can get messy as your store grows. If you’re using the new High-Performance Order Storage (HPOS) system, the default order list still doesn’t give you a quick way to filter by a custom date range.

In this guide, you’ll learn how to easily add a “From” and “To” date filter to your WooCommerce Admin Orders page. This lets you view only the orders placed within a specific time frame.

Some store owners notice their orders page slows down when filtering large datasets. If that happens to you, consider using Flexi Archiver to archive old orders while keeping data accessible.

Solution: Add a Date Range Filter to WooCommerce Admin Orders

The code below adds From/To date inputs to the WooCommerce Admin Orders page and filters orders within the selected date range, fully compatible with HPOS.

Note: This code snippet has been tested and works on WordPress 6.8.2 and WooCommerce 10.1.2.
add_action('woocommerce_order_list_table_restrict_manage_orders', 'ts_add_orders_date_range_filter', 15);
function ts_add_orders_date_range_filter() {
    ?>
    <label for="from_date"><?php esc_html_e('From Date:', 'woocommerce'); ?></label>
    <input type="date" id="from_date" name="from_date" value="<?php echo isset($_GET['from_date']) ? esc_attr($_GET['from_date']) : ''; ?>" />

    <label for="to_date"><?php esc_html_e('To Date:', 'woocommerce'); ?></label>
    <input type="date" id="to_date" name="to_date" value="<?php echo isset($_GET['to_date']) ? esc_attr($_GET['to_date']) : ''; ?>" />
    <?php
}

add_filter('woocommerce_order_list_table_prepare_items_query_args', 'ts_orders_from_date_range_filter');
function ts_orders_from_date_range_filter( $query_args ) {
    if ( !isset($query_args['date_query']) ) {
        $query_args['date_query'] = array();
    }

    if ( isset($_GET['from_date']) && !empty($_GET['from_date']) ) {
        $query_args['date_query'][] = array(
            'after'     => esc_attr($_GET['from_date']),
            'inclusive' => true,
        );
    }

    if ( isset($_GET['to_date']) && !empty($_GET['to_date']) ) {
        // Add one day to the date value
        $to_date = date('Y-m-d', strtotime( esc_attr($_GET['to_date']) ) ); 

        $query_args['date_query'][] = array(
            'before'    => $to_date,
            'inclusive' => true,
        );
    }
    return $query_args;
}

Output

When a date range is entered, the admin orders list filters to show only orders within that range.

Power users who need even more control over their Orders page can also add an “Order Origin” filter to WooCommerce admin orders page that helps you to quickly segment orders by source.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

2 thoughts on “How to Add a Date Range Filter to WooCommerce Admin Orders Page (HPOS Compatible)?

  1. Works absolutly fine, don’t need to buy specific module, thank you for the huge money and time gain !
    I fully recommand

Leave a Reply to Saranya Cancel reply

Your email address will not be published. Required fields are marked *

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible.

Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

By using our site, you acknowledge that you have read and understood our Privacy Policy and Terms & Conditions.