Search code examples
wordpresswordpress-hook

How to add search function in WordPress admin Users table for users custom field?


In my WordPress site, I've two users custom fields created via User Registration Plugin namely user_registration_user_district and user_registration_user_school. I wanted to add search functionality in Users list including these columns value as well.

I've added these custom fields as Column in WordPress Users list. Using following Code in my functions.php file

// Add new columns to the Users list
function add_custom_user_columns($columns) {
    $columns['user_registration_school_name'] = 'School';
    $columns['user_registration_user_district'] = 'District';
    return $columns;
}
add_filter('manage_users_columns', 'add_custom_user_columns');

// Display the custom column content
function show_custom_user_columns_content($value, $column_name, $user_id) {
    if ($column_name == 'user_registration_user_district') {
        return get_user_meta($user_id, 'user_registration_user_district', true);
    }
    if ($column_name == 'user_registration_school_name') {
        return get_user_meta($user_id, 'user_registration_school_name', true);
    }
    return $value;
}
add_filter('manage_users_custom_column', 'show_custom_user_columns_content', 10, 3);

I've added the following code in the function.php for search functionality.

add_filter('user_search_columns', 'add_user_registration_user_district_to_search');
function add_user_registration_user_district_to_search($search_columns) {
    $search_columns[] = 'user_registration_user_district';
    return $search_columns;
}

add_filter('pre_get_users', 'search_users_by_user_registration_user_district');
function search_users_by_user_registration_user_district($query) {
    if (is_admin() && $query->is_search() && isset($query->query_vars['search']) && !empty($query->query_vars['search'])) {
        $meta_query = array(
            'relation' => 'OR',
            array(
                'meta_key'     => 'user_registration_user_district',
                'meta_value'   => $query->query_vars['search'],
                'meta_compare' => 'LIKE',
            ),
        );
        $query->set('meta_query', $meta_query);
    }
}

But that didn't work. While searching the users list no data is returned. How to resolve this issue?


Solution

  • You're probably running into this problem because search might not be set up correctly to look through user meta fields, or the way you're adding the meta query could be messing with the existing search.

    You can try below snippet

    add_action('pre_user_query', function ($uqi) {
        global $wpdb;
    
        $search = '';
        if (isset($uqi->query_vars['search'])) {
            $search = trim($uqi->query_vars['search']);
        }
    
        if ($search) {
            $search = trim($search, '*');
            $the_search = '%' . $search . '%';
    
            // Define meta query for specific fields
            $search_meta = $wpdb->prepare("
                ID IN ( SELECT user_id FROM {$wpdb->usermeta}
                WHERE ( ( meta_key='user_registration_user_district' OR meta_key='user_registration_user_school' )
                    AND {$wpdb->usermeta}.meta_value LIKE %s )
                )", $the_search);
    
            // Add custom meta query condition to the user query
            $uqi->query_where = str_replace(
                'WHERE 1=1 AND (',
                "WHERE 1=1 AND (" . $search_meta . " OR ",
                $uqi->query_where
            );
        }
    });