WP_User_Query Custom Field meta_query with Date clause

You can set the type of the data in your meta_query.

Unfortunately, you can only use the comparison BETWEEN if your date is in the format YYYYMMDD. So to see the users for one year, you would have to set your metaquery like this:

$usersByCompanyArgs = new WP_User_Query(
    array(
        'fields' => 'all_with_meta',
        'orderby' => 'bhaa_runner_dateofrenewal',
        'order' => 'DESC',
        'meta_query' => array(
            array(
                'key' => 'bhaa_runner_company',
                'value' => $this->houseid,
                'compare' => '='),
            array(
                'key' => 'bhaa_runner_dateofrenewal',
                'value' => '2014-01-01', // date to compare to, before this one
                'compare' => '<',
                'type' => 'DATE' //set the format
            ),
            array(
                'key' => 'bhaa_runner_dateofrenewal',
                'value' => '2013-01-01', // date to compare to, after this one
                'compare' => '>=',
                'type' => 'DATE' //set the format
            ),
        )
    )
);

If your Date field was formated YYYYMMDD you could replace the two comparisons in the meta_query with

            array(
                'key' => 'bhaa_runner_dateofrenewal',
                'value' => array( '20130101', '20140101' ), // date to compare to, after this one
                'compare' => 'BETWEEN',
                'type' => 'DATE' //set the format
            )

Leave a Comment