27. June 2011
von Blackbam

Recently, when working on a WordPress website for a huge amount of users, a customer was unsatisfyied with the WordPress backend search for users. The default backend search only is searching for your search string in usernames, but not for first name, last name or email of users.

 

As most WordPress projects are barely designed to have a huge amoung of users, this is not necessary in most cases. Anyway, the following code will help with this problem - add this to your functions.php-file and the WordPress user search will scan first name, last name and e-mail of every user.

 

Download the official WordPress Plugin

How the Plugin basically works (code overview of Version 1.2)
 
   // the actual improvement of the query
    function user_search_by_multiple_parameters($wp_user_query) {
        if(false === strpos($wp_user_query -> query_where, '@') && !empty($_GET["s"])) {

            global $wpdb;

            $uids=array();

   // get the custom meta fields to search
   $iusib_custom_meta = get_option('iusib_meta_fields');
   $iusib_cma = array_map('trim', explode(",",$iusib_custom_meta));

   $iusib_add = "";
   // the escaped query string
   $qstr = mysql_real_escape_string($_GET["s"]);
   
   // add all custom fields into the query
   if(!empty($iusib_cma)) {
    $iusib_add = " OR meta_key='".implode("' OR meta_key='",$iusib_cma)."'";
   }

            $usermeta_affected_ids = $wpdb -> get_results("SELECT DISTINCT user_id FROM ".$wpdb->base_prefix."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->base_prefix."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;
    }
Share

Dieser Eintrag wurde am 27. June 2011 um 23:23 in der Kategorie Plugins, WordPress, WP Scripts veröffentlicht. You can book the comments for this article RSS 2.0. Feedback, discussion, commendation and critics are welcome: Write a comment or trackback.


Tags: , , ,

Already 74 comments belonging to "WordPress improved user search (first name, last name, email) in backend":

Kommentare abonnieren (RSS) or URL Trackback

Donty   says:

on 19. February 2013 at 13:44 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 19. February 2013 at 14:37 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 28. February 2013 at 23:11 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 01. March 2013 at 11:17 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 01. March 2013 at 18:01 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 01. March 2013 at 20:37 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 29. March 2013 at 00:48 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 29. March 2013 at 15:27 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 23. October 2013 at 02:51 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 05. November 2013 at 21:17 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 07. November 2013 at 16:10 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 08. January 2014 at 20:51 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 14. July 2014 at 20:27 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 15. July 2014 at 15:18 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 30. July 2014 at 17:44 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 31. July 2014 at 17:27 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 31. July 2014 at 19:24 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 31. July 2014 at 19:33 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 22. September 2014 at 22:10 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 03. October 2014 at 16:57 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 27. November 2014 at 10:15 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 11. April 2015 at 21:38 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 11. June 2015 at 12:11 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Donty   says:

on 14. June 2015 at 16:44 on clock


Hi Blackbam

Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.

Am I missing anything?

Thanks again

D

Leave a comment: