5

I would like to organise users by date registered every time an admin visits the users page, without having to click again from:

http://localhost:8888/wp-admin/users.php

I can add the date registered column and the sorting but I can't seem to find any documentation on how to pre sort the users without actually clicking the sort button on the column name.

I essentially want the users list to always be (unless selected otherwise):

/wp-admin/users.php?orderby=registered&order=asc
1
  • @PieterGoosen That is post ordering, I'm looking for user ordering. Thanks anyway. Tom
    – Thomas
    Commented Nov 24, 2015 at 9:47

3 Answers 3

9

This code will add the sortable user registration date column and it will handle default sorting of the user table by registration date.

The majority of this code was taken from Mika Epstein's Recently Registered plugin. I added the bit to handle the default sort order which is a tweaked snippet of code from this post.

/**
 * Registers users by date registered by default. When user clicks
 * other sortable column headers, those will take effect instead.
 */
add_action( 'pre_user_query', 'wpse209591_order_users_by_date_registered_by_default' );
function wpse209591_order_users_by_date_registered_by_default( $query ) {
    global $pagenow;

    if ( ! is_admin() || 'users.php' !== $pagenow || isset( $_GET['orderby'] ) ) {
        return;
    }
    $query->query_orderby = 'ORDER BY user_registered ASC';
}

/**
 * Registers column for display
 */
add_filter( 'manage_users_columns', 'wpse209591_users_columns');
function wpse209591_users_columns( $columns ) {
    $columns['registerdate'] = _x( 'Registered', 'user', 'your-text-domain' );
    return $columns;
}

/**
 * Handles the registered date column output.
 * 
 * This uses the same code as column_registered, which is why
 * the date isn't filterable.
 *
 * @global string $mode
 */
add_action( 'manage_users_custom_column', 'wpse209591_users_custom_column', 10, 3);
function wpse209591_users_custom_column( $value, $column_name, $user_id ) {

    global $mode;
    $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];

    if ( 'registerdate' != $column_name ) {
         return $value;
    } else {
        $user = get_userdata( $user_id );

        if ( is_multisite() && ( 'list' == $mode ) ) {
            $formated_date = __( 'Y/m/d' );
        } else {
            $formated_date = __( 'Y/m/d g:i:s a' );
        }

        $registered   = strtotime( get_date_from_gmt( $user->user_registered ) );
        $registerdate = '<span>'. date_i18n( $formated_date, $registered ) .'</span>' ;

        return $registerdate;
    }
}

/**
 * Makes the column sortable
 */
add_filter( 'manage_users_sortable_columns', 'wpse209591_users_sortable_columns' );
function wpse209591_users_sortable_columns( $columns ) {

    $custom = array(
        // meta column id => sortby value used in query
        'registerdate' => 'registered',
    );

    return wp_parse_args( $custom, $columns );
}

/**
 * Calculate the order if we sort by date.
 *
 */
add_filter( 'request', 'wpse209591_users_orderby_column' );
function wpse209591_users_orderby_column( $vars ) {

    if ( isset( $vars['orderby'] ) && 'registerdate' == $vars['orderby'] ) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'registerdate',
            'orderby' => 'meta_value'
        ) );
    }

    return $vars;
}
5

why so complicated? just use the default hook for that:

function change_user_order($args){
    $args["orderby"] = "user_registered";
    $args["order"] = "DESC";
    return $args;
}
add_filter("users_list_table_query_args","change_user_order");

from class-wp-user-query.php:

May be a single value, an array of values, or a multi-dimensional array with fields as keys and orders ('ASC' or 'DESC') as values. Accepted values are 'ID', 'display_name' (or 'name'), 'include', 'user_login' (or 'login'), 'login__in', 'user_nicename' (or 'nicename'), 'nicename__in', 'user_email (or 'email'), 'user_url' (or 'url'), 'user_registered' (or 'registered'), 'post_count', 'meta_value', 'meta_value_num', the value of $meta_key, or an array key of $meta_query. To use 'meta_value' or 'meta_value_num', $meta_key must be also be defined. Default 'user_login'.

1
  • Perfect, thanks! Commented Feb 16, 2022 at 12:46
4

If you want to overwrite the default setting, you have to check if the args are empty first:

function default_sort_users( $args ) {
    if ( empty( $args['orderby'] ) ) {
        $args['orderby'] = 'user_registered'; 
    }
    return $args;
}

add_filter( 'users_list_table_query_args', 'default_sort_users' );

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.