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

  • 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' );