WordPress:按数字排序管理员用户列

我想按自定义 ID 对用户概览列表中的管理列进行排序。为此,我添加了一个函数来对列进行排序,但它没有按预期工作。


它以一种奇怪的方式对字段进行排序。我想还有其他排序或函数有错误?!


这是为用户添加 ID 的代码:


add_action( 'edit_user_profile', 'wcv_store_custom_vendor_id', 0 );

//add_action( 'wcv_admin_after_store_address', 'wcv_store_custom_vendor_id' );

function wcv_store_custom_vendor_id( $user ) {

?>

    <table class="form-table">

        <tbody>

            <tr>

                <th><label for="_wcv_custom_vendor_id"><?php _e( 'Vendor ID', 'wcvendors-pro' ); ?></label></th>

                <td><input type="text" name="_wcv_custom_vendor_id" id="_wcv_custom_vendor_id" value="<?php echo get_user_meta( $user->ID, '_wcv_custom_vendor_id', true ); ?>" class="regular-text"></td>

            </tr>

        </tbody>

    </table>

<?php

}




// Save the details on the back end when updating the user

add_action( 'wcvendors_update_admin_user', 'save_wcv_custom_vendor_id' );

function save_wcv_custom_vendor_id( $user_id ){

    if ( isset( $_POST['_wcv_custom_vendor_id'] ) ) {

        update_user_meta( $user_id, '_wcv_custom_vendor_id', $_POST['_wcv_custom_vendor_id'] );

    }

这是我到目前为止所拥有的:


// Add column to admin

add_action('manage_users_columns', 'add_custom_vendor_id_column', 10, 1 );

function add_custom_vendor_id_column( $columns ) {

    $columns['wcv_custom_vendor_id'] = __('Vendor-ID');

    return $columns;

}



// fetching the status, thanks to LoicTheAztec

add_filter('manage_users_custom_column',  'add_data_to_vendor_id_column', 10, 3);

function add_data_to_vendor_id_column( $value, $column_name, $user_id ) {

    if ( 'wcv_custom_vendor_id' == $column_name ) {

        if( get_user_meta( $user_id, '_wcv_custom_vendor_id', true ) != '' ) {

            $value = '<span style="color:green;font-weight:bold;">'.get_user_meta( $user_id, '_wcv_custom_vendor_id', true ).'</span>';

        } else {

            $value = '<span class="" style="color:red;font-weight:bold;">No ID!</span>';

        }

    }

    return $value;

}

我检查了很多片段,最后一个函数应该按数字排序。但我认为它根本没有影响。


有什么我想念的吗?


慕容森
浏览 105回答 1
1回答

qq_花开花谢_0

pre_get_users反面使用request对于meta_key我们使用以下的空值:update_user_meta( $user_id, '_wcv_custom_vendor_id', 0 );所以你得到:// Add column to adminfunction add_custom_vendor_id_column( $columns ) {&nbsp;&nbsp;&nbsp; &nbsp; $columns['wcv_custom_vendor_id'] = __( 'Vendor-ID', 'woocommerce');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return $columns;}add_action( 'manage_users_columns', 'add_custom_vendor_id_column', 10, 1 );// fetching the status, thanks to LoicTheAztecfunction add_data_to_vendor_id_column( $value, $column_name, $user_id ) {&nbsp; &nbsp; if ( $column_name == 'wcv_custom_vendor_id' ) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $vendor_id = (int) get_user_meta( $user_id, '_wcv_custom_vendor_id', true );&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // If empty&nbsp; &nbsp; &nbsp; &nbsp; if ( empty ($vendor_id ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; update_user_meta( $user_id, '_wcv_custom_vendor_id', 0 );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if ( $vendor_id && $vendor_id != 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $value .= '<span style="color:green;font-weight:bold;">' . $vendor_id . '</span>';&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $value = '<span class="" style="color:red;font-weight:bold;">No ID!</span>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return $value;}add_filter( 'manage_users_custom_column', 'add_data_to_vendor_id_column', 10, 3);// make admin colum sortablefunction fc_my_sortable_cake_column( $columns ) {&nbsp; &nbsp;&nbsp; &nbsp; $columns['wcv_custom_vendor_id'] = __( 'Vendor-ID', 'woocommerce');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return $columns;}add_filter( 'manage_users_sortable_columns', 'fc_my_sortable_cake_column', 10, 1 );// orderbyfunction action_pre_get_users( $query ) {&nbsp; &nbsp; if ( !is_admin() )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; $orderby = $query->get('orderby');&nbsp; &nbsp; if ( $orderby == 'Vendor-ID' ) {&nbsp; &nbsp; &nbsp; &nbsp; $query->set( 'orderby' , 'meta_value_num' );&nbsp; &nbsp; &nbsp; &nbsp; $query->set( 'meta_key', '_wcv_custom_vendor_id' );&nbsp; &nbsp; }}add_action( 'pre_get_users', 'action_pre_get_users', 10, 1 );
打开App,查看更多内容
随时随地看视频慕课网APP