我想通过自定义 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;
}
// make admin colum sortable
function fc_my_sortable_cake_column( $columns ) {
$columns['wcv_custom_vendor_id'] = 'Vendor-ID';
return $columns;
}
add_filter( 'manage_users_sortable_columns', 'fc_my_sortable_cake_column' );
// second try to sort by number, no effect?!
function sort_datanowa_column( $vars ) {
if ( isset( $vars['orderby'] ) && 'Vendor-ID' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => '_wcv_custom_vendor_id', // Find correct meta field key
'orderby' => 'meta_value_num'
) );
}
return $vars;
}
add_filter( 'request', 'sort_datanowa_column' );
我检查了很多片段,最后一个函数应该按数字排序。但我认为它根本没有效果。
有什么我想念的吗?