Created
May 11, 2016 19:50
-
-
Save sectsect/a42650e871cf78e94de1c63a03745443 to your computer and use it in GitHub Desktop.
Wordpress: Sort users by numeric value in Users Screen @ https://foxland.fi/sort-users-by-numeric-value-in-users-screen/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add new user column: Expire Date. | |
* | |
* @since 1.0.0 | |
* @return array $columns | |
*/ | |
function prefix_expire_date_column( $columns ) { | |
$columns['expire_date'] = __( 'Expire Date', 'text-domain' ); | |
return $columns; | |
} | |
add_filter( 'manage_users_columns', 'prefix_expire_date_column' ); | |
/** | |
* Adds Expire date and Status to column. | |
* | |
* @since 1.0.0 | |
* @return void | |
*/ | |
function prefix_add_custom_columns( $value, $column_name, $user_id ) { | |
if( 'expire_date' == $column_name ) { | |
/* Get expire date somehow. For example using your custom function */ | |
// $expire_date = prefix_get_expire_date( $user_id ); | |
$expire_date = $user_id + 1; | |
$value = $expire_date; | |
} | |
return $value; | |
} | |
add_action( 'manage_users_custom_column', 'prefix_add_custom_columns', 10, 3 ); | |
/** | |
* Add sortable columns. | |
* | |
* @since 1.0.0 | |
* @return void | |
*/ | |
function prefix_sortable_columns( $columns ) { | |
$columns['expire_date'] = 'expire_date'; | |
return $columns; | |
} | |
add_filter( 'manage_users_sortable_columns', 'prefix_sortable_columns' ); | |
/** | |
* Sort by expire date. Meta key is called 'prefix_expiration_date'. | |
* | |
* @since 1.0.0 | |
* @return void | |
*/ | |
function prefix_sort_by_expiration_date( $query ) { | |
if ( 'expire_date' == $query->get( 'orderby' ) ) { | |
$query->set( 'orderby', 'meta_value_num' ); | |
$query->set( 'meta_key', 'prefix_expiration_date' ); | |
} | |
} | |
add_action( 'pre_get_users', 'prefix_sort_by_expiration_date' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment