Have a client with almost 2500 users in their WordPress site and they wanted to be able to search their users by last name. By default this is not enabled in WordPress core, so here is a quick function/hook you can add to your functions.php file to enable this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
function custom_user_list_search( $query ) { if ( is_admin() ) { global $wpdb; if ( is_array( $qv = $query->query_vars ) && ( $s = trim( $qv['search'], '* \t\n\0\x0B' ) ) && ( $s = "%" . esc_sql( like_escape( $s ) ) . "%" ) ) { if ( !preg_match( '/' . $wpdb->usermeta . '/', $query->query_from ) ) $query->query_from .= " INNER JOIN `" . $wpdb->usermeta . "` ON `" . $wpdb->users . "`.`ID` = `" . $wpdb->usermeta . "`.`user_id`"; $query->query_where .= " OR ( `" . $wpdb->usermeta . "`.`meta_key` = 'last_name' AND `" . $wpdb->usermeta . "`.`meta_value` LIKE '" . $s . "' ) "; } } return $query; } add_filter( 'pre_user_query', 'custom_user_list_search', 15 ); |
I will probably expand this a little and create a WordPress plugin for it (if I can find the time).
NOTE: The trim regex should be this – ‘* \t\n\r\0\x0B’ – but the plugin I’m using is stripping the \r