4

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?

1 Answer 1

9

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();
}
2
  • what does 'inclusive' => true, do in this query?
    – Q Studio
    Commented Sep 13, 2022 at 10:08
  • 1
    From docs: inclusive (boolean) – For after/before, whether exact value should be matched or not’.
    – birgire
    Commented Sep 13, 2022 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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