Created
October 12, 2018 14:47
-
-
Save actual-saurabh/4677c7845ee49c484d1197a6e65c317e to your computer and use it in GitHub Desktop.
Order Users Table by LifterLMS Memberships (Draft)
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 // Do not copy this line | |
| // Copy from under this line and paste into your child theme's functions.php | |
| /* | |
| * This code is not perfect and returns more than one result for the same user. | |
| * I'll take another stab at this to fix that. | |
| * Also, this is not likely to work that well in case a user is enrolled in more than one membership. | |
| */ | |
| add_filter( 'manage_users_sortable_columns', 'llms_make_membership_column_sortable' ); | |
| function llms_make_membership_column_sortable( $columns ) { | |
| $columns['llms-memberships'] = 'llms-memberships'; | |
| return $columns; | |
| } | |
| add_action( 'pre_user_query', 'llms_sort_users_table_by_membership' ); | |
| function llms_sort_users_table_by_membership( $wp_user_query ) { | |
| if( !is_admin() ){ | |
| return $wp_user_query; | |
| } | |
| if ( $wp_user_query->query_vars['orderby'] !='llms-memberships' ){ | |
| return $wp_user_query; | |
| } | |
| global $wpdb; | |
| $prefix = $wpdb->prefix; | |
| $order = $wp_user_query->query_vars['order']; | |
| $user_postmeta_table = $prefix.'lifterlms_user_postmeta'; | |
| $users_table = $prefix.'users'; | |
| $posts_table = $prefix.'posts'; | |
| $wp_user_query->query_from .= " LEFT JOIN $user_postmeta_table ON $users_table.ID=$user_postmeta_table.user_id LEFT JOIN $posts_table ON $user_postmeta_table.post_id = $posts_table.ID"; | |
| $wp_user_query->query_orderby = "ORDER BY $posts_table.post_title $order GROUP BY $users_table.ID"; | |
| return $wp_user_query; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment