Skip to content

Instantly share code, notes, and snippets.

@norcross
Created February 19, 2013 20:12
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norcross/4989464 to your computer and use it in GitHub Desktop.
Save norcross/4989464 to your computer and use it in GitHub Desktop.
add first name, last name, and email to back-end WP user search
<?php
add_action ( 'pre_user_query', 'rkv_user_search' );
function rkv_user_search($wp_user_query) {
if(false === strpos($wp_user_query->query_where, '@') && !empty($_GET["s"])) {
global $wpdb;
$uids = array();
$iusib_add = '';
// the escaped query string
$qstr = mysql_real_escape_string($_GET["s"]);
$usermeta_affected_ids = $wpdb->get_results("
SELECT DISTINCT user_id
FROM $wpdb->usermeta
WHERE (meta_key='first_name' OR meta_key='last_name'".$iusib_add.")
AND LOWER(meta_value) LIKE '%".$qstr."%'
");
foreach($usermeta_affected_ids as $maf) {
array_push($uids,$maf->user_id);
}
$users_affected_ids = $wpdb->get_results("
SELECT DISTINCT ID FROM $wpdb->users
WHERE LOWER(user_nicename)
LIKE '%".$qstr."%'
OR LOWER(user_email)
LIKE '%".$qstr."%'
");
foreach($users_affected_ids as $maf) {
if(!in_array($maf->ID,$uids)) {
array_push($uids,$maf->ID);
}
}
$id_string = implode(",",$uids);
$wp_user_query->query_where = str_replace("user_nicename LIKE '%".$qstr."%'", "ID IN(".$id_string.")", $wp_user_query->query_where);
}
return $wp_user_query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment