为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕容森 2023-04-28 14:56:25
我想按自定义 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 useradd_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 adminadd_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 LoicTheAztecadd_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;}我检查了很多片段,最后一个函数应该按数字排序。但我认为它根本没有影响。有什么我想念的吗?
查看完整描述

1 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

  • pre_get_users反面使用request

  • 对于meta_key我们使用以下的空值:update_user_meta( $user_id, '_wcv_custom_vendor_id', 0 );

所以你得到:

// Add column to admin

function add_custom_vendor_id_column( $columns ) {  

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

    

    return $columns;

}

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


// fetching the status, thanks to LoicTheAztec

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

    if ( $column_name == 'wcv_custom_vendor_id' ) {

        

        $vendor_id = (int) get_user_meta( $user_id, '_wcv_custom_vendor_id', true );

        

        // If empty

        if ( empty ($vendor_id ) ) {

            update_user_meta( $user_id, '_wcv_custom_vendor_id', 0 );

        }

        

        if ( $vendor_id && $vendor_id != 0 ) {          

            $value .= '<span style="color:green;font-weight:bold;">' . $vendor_id . '</span>';

        } else {

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

        }

    }

    

    return $value;

}

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


// make admin colum sortable

function fc_my_sortable_cake_column( $columns ) {   

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

    

    return $columns;

}

add_filter( 'manage_users_sortable_columns', 'fc_my_sortable_cake_column', 10, 1 );


// orderby

function action_pre_get_users( $query ) {

    if ( !is_admin() )

        return;


    $orderby = $query->get('orderby');


    if ( $orderby == 'Vendor-ID' ) {

        $query->set( 'orderby' , 'meta_value_num' );

        $query->set( 'meta_key', '_wcv_custom_vendor_id' );

    }

}

add_action( 'pre_get_users', 'action_pre_get_users', 10, 1 );


查看完整回答
反对 回复 2023-04-28
  • 1 回答
  • 0 关注
  • 104 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信