3

I'm trying to get the number of user who registered today.

I've tried this:

$args = array (
'role'           => 'Subscriber',
'meta_query'     => array(
    array(
        'key'       => 'user_registered',
        'value'     => '2015-01-13 00:00:00',
        'compare'   => '>=',
        'type'      => 'DATE', //also tried DATETIME, TIME
    ),
 ),
);

$user_query = new WP_User_Query( $args );
$user_query->total_users;

But the results is always 0.

This works:

global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM `wp_users` WHERE `user_registered` >= '2015-01-13 00:00:00'");
echo count($results);

But shouldn't WP_User_Query() be able to do this. Am I doing something wrong with the args?

4

You can simply use the date_query parameter on the user registration date:

$args = array (
    'role'          => 'subscriber',
    'date_query'    => array(
        array(
            'after'     => '2010-01-13 00:00:00',
            'inclusive' => true,
        ),
     ),
);

$user_query = new WP_User_Query( $args );

This part of the WP_User_Query source code, makes it possible:

// Date queries are allowed for the user_registered field.
if ( ! empty( $qv['date_query'] ) && is_array( $qv['date_query'] ) ) {
    $date_query = new WP_Date_Query( $qv['date_query'], 'user_registered' );
    $this->query_where .= $date_query->get_sql();
}

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.