I created a new column in the Users Admin Tab called "Listings". Each line is a number which means how many Listings an user has.
//// ADD THE NEW COLUMN
add_filter( 'manage_users_columns', 'add_listing_count_column' );
function add_listing_count_column( $columns ) {
$columns['Listings'] = 'Listings'; //
return $columns;}
// FILL THE NEW COLUMN
add_filter( 'manage_users_custom_column', 'add_listing_count_column_row', 10, 3 );
function add_listing_count_column_row( $row_output, $column_id_attr, $user_id ) {
if ( $column_id_attr == 'Listings') {
return count_user_posts( $user_id, 'job_listing' );
}
return $row_output;}
I want to make that column sortable.. But it keeps sorting by username... Any idea what could it be?
// MAKE THE NEW COLUMN SORTABLE
add_filter( 'manage_users_sortable_columns', 'add_listing_count_column_sortable',10,3 );
function add_listing_count_column_sortable( $columns ) {
return wp_parse_args( array( 'Listings' => 'Listings' ), $columns );
}
add_action( 'pre_get_users', 'smartwp_sort_last_login_column' );
function smartwp_sort_last_login_column( $query ) {
if ( ! is_admin() ) {
return $query;
}
$screen = get_current_screen();
if ( isset( $screen->id ) && $screen->id !== 'users' ) {
return $query;
}
if ( isset( $_GET[ 'orderby' ] ) && $_GET[ 'orderby' ] == 'Listings' ) {
$query->query_vars['orderby'] = 'post_count';
}
return $query;
}
thank you