Filtering users in the WordPress admin panel is helpful when managing a large number of users. For instance, if you need to send invites to specific users, a filter in the users page will be handy for you. In this guide, we will add a filter dropdown to the users list in the WordPress admin to filter users based on user roles. This code will filter “Premium Member” and “Subscriber” user roles.
Note: Please note that in this demo we are using the user role field similar to WordPress’s default role editor field, but this is just an example. You can replace it with any field (e.g., Subscription status, Location, or Gender).
<?php /** * Add extra user profile field (User Role Selection) */ function ts_extra_user_profile_fields( $user ) { $user_role = get_user_meta( $user->ID, 'user_role_selection', true ); ?> <h3><?php _e( 'Extra Profile Fields', 'default' ); ?></h3> <table class="form-table"> <tr> <th><label for="user_role_selection"><?php _e( 'User Role' ); ?></label></th> <td> <select name="user_role_selection"> <option value="">Select Role</option> <option <?php selected( $user_role, 'premium_member' ); ?> value="premium_member">Premium Member</option> <option <?php selected( $user_role, 'subscriber' ); ?> value="subscriber">Subscriber</option> </select> <p class="description"><?php _e( 'Select the role for this user.', 'default' ); ?></p> </td> </tr> </table> <?php } add_action( 'show_user_profile', 'ts_extra_user_profile_fields' ); add_action( 'edit_user_profile', 'ts_extra_user_profile_fields' ); /** * Save extra user profile field. */ function ts_save_extra_user_profile_fields( $user_id ) { if ( ! current_user_can( 'edit_user', $user_id ) ) { return false; } if ( isset( $_POST['user_role_selection'] ) ) { update_user_meta( $user_id, 'user_role_selection', sanitize_text_field( $_POST['user_role_selection'] ) ); } } add_action( 'personal_options_update', 'ts_save_extra_user_profile_fields' ); add_action( 'edit_user_profile_update', 'ts_save_extra_user_profile_fields' ); /** * Add filter dropdown to Users list in the WordPress admin. */ function ts_user_role_filter( $which ) { if ( 'top' === $which ) { $value = isset( $_GET['user_role'] ) ? $_GET['user_role'] : ''; ?> <select name="user_role"> <option value="">Filter by Role</option> <option value="premium_member" <?php selected( $value, 'premium_member' ); ?>>Premium Member</option> <option value="subscriber" <?php selected( $value, 'subscriber' ); ?>>Subscriber</option> </select> <?php submit_button( 'Filter', '', '', false ); } } add_action( 'manage_users_extra_tablenav', 'ts_user_role_filter' ); /** * Modify the user query based on the selected filter. */ function ts_filter_users_by_role( $query ) { if ( is_admin() && isset( $_GET['user_role'] ) && ! empty( $_GET['user_role'] ) ) { $query->set( 'meta_query', array( array( 'key' => 'user_role_selection', 'compare' => '=', 'value' => sanitize_text_field( $_GET['user_role'] ), ), ) ); } } add_action( 'pre_get_users', 'ts_filter_users_by_role' ); ?>