1 回答
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 );
- 1 回答
- 0 关注
- 104 浏览
添加回答
举报