2

I was expecting to be able to do something like the following which I thought would return all users that have registered before a given date:

$args = array(
    'post_type' => 'post',
    'date_query' => array(
        array(
            'before' => current_time( 'mysql' )
        )
    )
);
$query = new WP_User_Query( $args );

However, unlike WP_Query, WP_User_Query doesn't support the date_query parameter so the above code doesn't work.

How can I get a list of all users registered before a given date?

1
  • 1
    This is now supported since 4.1. I updated the Codex when I was working on this answer.
    – birgire
    Commented Aug 11, 2015 at 13:00

2 Answers 2

5

Method #1: The pre_user_query hook:

There are not many filters available, but you can try the pre_user_query hook:

// Add filter:
add_action(    'pre_user_query', 'wpse_filter_by_reg_date' );

// Query:
$query = new WP_User_Query( $args );

// Remove filter:
remove_action( 'pre_user_query', 'wpse_filter_by_reg_date' );

where the filter callback is:

/**
 * Filter WP_User_Query by user_registered date
 *
 * @see   http://wordpress.stackexchange.com/a/160826/26350
 * @param WP_User_Query The current WP_User_Query instance
 */

function wpse_filter_by_reg_date( $q )
{
    global $wpdb;
    $q->query_where .=  $wpdb->prepare(
        " AND {$wpdb->users}.user_registered >= '%s' ",
        date( "Y-m-d" )
    );
}

You can then modify the filter callback to your needs.

Method #2: WP_User_Query with custom query variables:

You can also make this more dynamic, by using the custom string parameters _registered and _registered_compare. Then your query could be:

$args = array(
    'orderby'             => 'login',
    'order'               => 'ASC',
    '_registered'         => date( 'Y-m-d' ),
    '_registered_compare' => '>=',
);
$query = new WP_User_Query( $args );

where:

/**
 * Add support for the custom '_registered' and '_registered_compare'
 * string input parameters in WP_User_Query().
 *
 * @see   http://wordpress.stackexchange.com/a/160826/26350
 * @param WP_User_Query The current WP_User_Query instance
 */

function wpse_registered_filter( $q )
{
    if( isset( $q->query_vars['_registered'] )
        && isset( $q->query_vars['_registered_compare'] )
        && is_string( $q->query_vars['_registered_compare'] )
    )
    {   
        // Input:
        $registered = $q->query_vars['_registered'];
        $compare    = $q->query_vars['_registered_compare'];

        // Init:     
        $available_compares = array( '=', '<', '>', '<=', '>=', '!=' );

        // Default compare:
        if( ! in_array( $compare, $available_compares, TRUE ) )
            $compare = '=';

        // Modify query:
        global $wpdb;                                               
        $q->query_where .=  $wpdb->prepare(
            " AND {$wpdb->users}.user_registered {$compare} '%s' ",
            $registered
        );
    }
}
add_action( 'pre_user_query', 'wpse_registered_filter' );

Method #3: Manual SQL:

Responding to the comment below, this is an example how you can write the SQL manually:

    global $wpdb;
    $sql = $wpdb->prepare(
        "SELECT ID FROM {$wpdb->users} WHERE {$wpdb->users}.user_registered >= '%s' ",
        current_time( 'mysql' )
    );
    $uids = $wpdb->get_col( $sql );
8
  • Think your approach might be faster than mine :-) Commented Sep 9, 2014 at 19:30
  • 1
    Maybe, but some prefer to work in PHP instead of SQL, hence your solution ;-)
    – birgire
    Commented Sep 9, 2014 at 19:31
  • Thanks @birgire. Is there any advantage of the WP_User_Query... pre_user_query approach over a simple custom select query? Commented Sep 9, 2014 at 21:58
  • I think you could ask the same about WP_Query + hooks versus a custom SQL query ;-) I usually prefer the "flexibility" of WP_Query or WP_User_Query in this case. Additionally the output of WP_User_Query is an array of WP_User objects.
    – birgire
    Commented Sep 9, 2014 at 22:05
  • 1
    I updated the answer with a manual SQL query. ps: the default offset is 0 for get_col().
    – birgire
    Commented Sep 9, 2014 at 22:33
3

You can make use of WP_User_Query to retrieve a list of all user's ID's and then pass that to get_userdata to retrieve a user's registration date.

You can then sort and exclude users from that according to a specific date and time.

Here is my idea

<?php
    $user_query = new WP_User_Query( array(
        'fields'    => 'ID',
    ) );
    $users = $user_query->get_results();

    foreach( $users as $user ) {
        $user_object = get_userdata( $user );
        $cutoffdate = '2013-07-01 00:00:01';

        if( $user_object->user_registered < $cutoffdate ) {
            echo '<p>' . $user_object->display_name; 
            echo '</br>';
            echo $user_object->user_registered . '</p>';
        }
    }
?>
2
  • 2
    Thanks Pieter, a nice approach. As you point out in the comments on @birgire's answer, it may be a slightly slower way of doing it, especially if there are many users Commented Sep 9, 2014 at 22:00
  • I totally agree. I just tried to show how it is done without custom sql, but to be honest, I like @birgire solution more and would also choose his above mine :-) Just glad his solution panned out for you. Enjoy Commented Sep 10, 2014 at 4:25

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.