Home Forums Backend Issues (wp-admin) Extend backend User search to custom user meta


Extend backend User search to custom user meta

  • How would I extend the search feature on the backend Users listing to look in ACF custom user fields, as well as default user fields?

    I’m not too interested in a plugin like Relevanssi or anything that would carry this out on a separate page.

  • This was going to be a very long comment but then I found something while I was looking for information and it just made sense to give you a link

  • Struggling to understand it a little bit – mainly how it builds the query AND THEN decides what fields to include in the search. But I guess that’s just SQL.

    I am seeing results with a basic query.

    Problem is, paginated results are only showing two per page.
    In fairness, that was also a problem I experienced when using a plugin, Better User Search, which was also built to allow a user to specify custom fields to search. I am now wondering if there is another reason why results in both methods are being pegged to just two per page.

    Indeed, I have just found this issue relating to the “Number of items per page” count set in screen Settings…

        Was 150: only two displayed
        When 200: only two displayed
        When 400: four are displayed
        When 500: four are displayed
        When 600: five are displayed
        When 700: six are displayed
        When 800: six are displayed

    I’ve stripped out other functions that could be causing a conflict and isolated the one in the code you linked to…

         function rudr_extend_user_search( $u_query ){
         	// make sure that this code will be applied only for user search
         	if ( $u_query->query_vars['search'] ){
         		$search_query = trim( $u_query->query_vars['search'], '*' );
         		if ( $_REQUEST['s'] == $search_query ){
         			global $wpdb;
          			// let's search by users first name
         			$u_query->query_from .= " JOIN {$wpdb->usermeta} fname ON fname.user_id = {$wpdb->users}.ID AND fname.meta_key = 'first_name'";
         			// you can add here any meta key you want to search by
         			// $u_query->query_from .= " JOIN {$wpdb->usermeta} cstm ON cstm.user_id = {$wpdb->users}.ID AND cstm.meta_key = 'YOU CUSTOM meta_key'";
          			// let's search by all the post titles, the user has been published
         			// $u_query->query_from .= " JOIN {$wpdb->posts} psts ON psts.post_author = {$wpdb->users}.ID";
          			// what fields to include in the search
          			$search_by = array( 'user_login' );
          			// apply to the query
         			$u_query->query_where = 'WHERE 1=1' . $u_query->get_search_sql( $search_query, $search_by, 'both' );
  • The only thing I can thing of is that you’ve got some other filter limiting the number. Look for a pre_get_users filter in your theme and plugins.

  • Update for the public record…

    I tried both the Better User Search plugin and the code I linked to on a single-install WordPress and it worked fine, but it won’t work on my Multisite, even with the theme switched to default and pretty much everything disabled.

    I ran “grep -lr “pre_get_users” *” in both the plugins and my theme folder.
    pre_get_users turned up in my Admin Columns Pro plugin – but that plugin is actually disabled.

  • If it has to do with mutlisite, when WP get’s users it only gets them from the current blog. Are all the users that you’re searching for part of the current blog? Honestly, I’m not sure what all the differences are, but it has to be something specific to multisite. All I know from a quick look at WP_User_Query is that that the blog ID alters the query in several ways. This could be something you can alter using a pre_get_users filter?

  • Yes, all the Users being searched are only in the blog in question. Apart from me as super-admin, I suppose.

    Blog ID is 2.

    pre_get_users is already a key part of both methods of performing the extended user search (Better User Search and the Misha code you linked to. So, I don’t know whether it’s the problem and/or solution here.

    Some learning to do, I’d say.

  • You might want to see if the developer of either of those plugins can give you any advice.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Extend backend User search to custom user meta’ is closed to new replies.