6

I'm trying to use meta_query to return all users that have registered today :

$args = array(
    'meta_query' => array(
        array(
            'key' => 'user_registered',
            'value' => date('Y-m-d'),
            'compare' => '=',
            'type' => 'DATE'
        )
    )
);

$query = new WP_User_Query( $args );
$users = $query->get_results();

This doesn't seem to work though. What am I missing ?

  • 1
    Yes, there were changes made in 4.1 and the WP_User_Query class now supports a WP_Date_Query query on the user registration date. I used it in my answer here and updated the Codex at that time. – birgire Nov 6 '15 at 13:54
  • Hi, @mike23 - There's now a more up to date answer to your question below. Would you mind changing the accepted answer for the benefit of future Googlers? – SinisterBeard Nov 6 '15 at 16:03
6
+100

Few months ago, I updated the Codex for get_users() and WP_User_Query, regarding the date_query support on the user's registration date, in WordPress 4.1+.

Then I also added a simple example on how to find users that registered during the last 12 hours.

But the current question is how to find users that registered today.

We could simply use relative date string:

$args = [
    'date_query' => [
        [ 'after'  => 'today', 'inclusive' => true ],
    ] 
];

$query = new WP_User_Query( $args );

that will generate the following SQL query:

SELECT SQL_CALC_FOUND_ROWS wp_users.* FROM wp_users WHERE 1=1 AND ( 
    wp_users.user_registered >= '2015-11-06 00:00:00'
) ORDER BY user_login ASC ;

We might also set the year, month and day parameters explicitly:

$args = [
    'date_query' => [
        [   
            'year'  => current_time( 'Y' ),
            'month' => current_time( 'm' ),
            'day'   => current_time( 'd' ),
        ],
    ] 
];

$query = new WP_User_Query( $args );

that will generate:

SELECT SQL_CALC_FOUND_ROWS wp_users.* FROM wp_users WHERE 1=1 AND ( 
    ( 
        YEAR( wp_users.user_registered ) = 2015 
        AND MONTH( wp_users.user_registered ) = 11 
        AND DAYOFMONTH( wp_users.user_registered ) = 6 
    )
) ORDER BY user_login ASC ;
11

The field user_registered is not a in the *_usermeta table, but the in *_users.

Nor do there seem to be any appropriate hooks to alter the SQL statement when using WP_User_Query. It seems you will need to use a direct SQL statement.

Another (minor) complication is that the registered date is stored as a date-time.

function wpse51485_get_users_registered_on( $date='' ){
    global $wpdb;

    if( empty($date) )
        $date = date('Y-m-d');

    $morning = new DateTime($date. ' 00:00:00');
    $night = new DateTime($date.' 23:59:59'); 
    $m = $morning->format('Y-m-d H:i:s');
    $n = $night->format('Y-m-d H:i:s');

    $sql = $wpdb->prepare("SELECT wp_users.* FROM wp_users WHERE 1=1 AND CAST(user_registered AS DATE) BETWEEN %s AND %s ORDER BY user_login ASC",$m,$n);

    $users = $wpdb->get_results($sql);

    return $users;
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.