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…
I’ve stripped out other functions that could be causing a conflict and isolated the one in the code you linked to…
add_action('pre_user_query','rudr_extend_user_search');
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' );
}
}
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.