1

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
  • @PieterGoosen That is post ordering, I'm looking for user ordering. Thanks anyway. Tom – Thomas Nov 24 '15 at 9:47
1

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;
}

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.