php - How to sort by custom column in users section in worpdress -
i have 2 custom columns in users section in word press "store name" , " date" these 2 saved in users_meta table in db , how can show data in user admin list sorted "store name" , "date". current code till add column heading , fetching them user_meta in users section follow.
themes function.php
/* adds store name , date column user display dashboard. * * @param $columns array of columns displayed on user dashboard * @return updated array of columns including zip codes. */ function theme_add_user_store_name_column( $columns ) { $columns['store_name'] = __( 'store name', 'theme' ); $columns['date'] = __( 'date', 'theme' ); return $columns; } // end theme_add_user_store_name_column add_filter( 'manage_users_columns', 'theme_add_user_store_name_column' ); /** * populates store name , date column specified user's store name. * * @param $value empty string * @param $column_name name of column populate * @param $user_id id of user we're working * @return store name associated user */ function theme_show_user_store_name_data( $value, $column_name, $user_id ) { if( 'store_name' == $column_name ) { return get_user_meta( $user_id, 'store_name', true ); } if( 'date' == $column_name ) { return get_user_meta( $user_id, 'date', true ); }// end if } // end theme_show_user_store_name_data add_action( 'manage_users_custom_column', 'theme_show_user_store_name_data', 10, 3 );
use following code sort users table according custom meta field, used single column, can modify add second field well.
// register column sortable function store_name_column_register_sortable( $columns ) { $columns['store_name'] = 'store_name'; return $columns; } add_filter( 'manage_users_sortable_columns', 'store_name_column_register_sortable' ); function store_name_column_orderby( $vars ) { if ( isset( $vars['orderby'] ) && 'store_name' == $vars['orderby'] ) { $vars = array_merge( $vars, array( 'meta_key' => 'store_name', 'orderby' => 'meta_value' ) ); } return $vars; } add_filter( 'request', 'store_name_column_orderby' );
Comments
Post a Comment