Created
February 19, 2013 20:12
-
-
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
This file contains 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 | |
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