您可以按特定列预先对用户列表进行排序吗?

时间:2015-11-23 作者:Thomas

我希望在管理员每次访问用户页面时按注册日期组织用户,无需再次单击:

http://localhost:8888/wp-admin/users.php
我可以添加“注册日期”列和排序,但如果不实际单击列名上的排序按钮,我似乎找不到任何关于如何对用户进行预排序的文档。

我基本上希望用户列表始终为(除非另有选择):

/wp-admin/users.php?orderby=registered&order=asc

3 个回复
SO网友:Dave Romsey

此代码将添加可排序用户注册日期列,并将按注册日期处理用户表的默认排序。

此代码的大部分来自Mika Epstein\'s Recently Registered plugin. 我添加了位来处理默认的排序顺序,这是来自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;
}

SO网友:Nic Bug

为什么这么复杂?只需使用默认挂钩即可:

function change_user_order($args){
    $args["orderby"] = "user_registered";
    $args["order"] = "DESC";
    return $args;
}
add_filter("users_list_table_query_args","change_user_order");
来自wp类用户查询。php:

可以是单个值、值数组或多维数组,其中字段作为键,顺序(“ASC”或“DESC”)作为值。接受的值为“ID”、“display\\u name”(或“name”)、“include”、“user\\u login”(或“login”)、“login\\u in”、“user\\u nicename”(或“nicename”)、“nicename\\u in”、“user\\u email(或“email”)、“user\\u url”(或“url”)、“user\\u registered”(或“registered”)、“post\\u count”、“meta\\u value”、“meta\\u value\\u num”、的值$meta_key, 或数组键$meta_query. 要使用“meta\\u value”或“meta\\u value\\u num”,$meta_key 还必须定义。默认“user\\u login”。

SO网友:Peter Højlund Andersen

如果要覆盖默认设置,必须先检查参数是否为空:

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\' );