<?php
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
$search_term = sanitize_text_field($_GET['s']);
$only_fields = array( 'user_login', 'user_nicename', 'user_email','ID' );
$count_args = array(
'role' => 'enter-custom-user-role',
'fields' => $only_fields,
'search' => '*'.esc_attr( $search_term ).'*',
'number' => 999999
);
$user_count_query = new WP_User_Query($count_args);
$user_count = $user_count_query->get_results();
// count the number of users found in the query
$total_users = $user_count ? count($user_count) : 1;
// grab the current page number and set to 1 if no page number is set
$page = isset($_GET['p']) ? $_GET['p'] : 1;
// how many users to show per page
$users_per_page = 5;
// calculate the total number of pages.
$total_pages = 1;
$offset = $users_per_page * ($page - 1);
$total_pages = ceil($total_users / $users_per_page);
$user_fields = array( 'user_login', 'user_nicename', 'user_email','ID' );
// main user query
$args = array(
// search only for Authors role
'role' => 'enter-custom-user-role',
// order results by display_name
'orderby' => 'display_name',
// return all fields
'fields' => $user_fields,
'number' => $users_per_page,
'order' => 'ASC',
'search' => '*'.esc_attr( $search_term ).'*',
'offset' => $offset // skip the number of users that we have per page
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query($args);
// Get the results
$all_agents = $wp_user_query->get_results();
// grab the current query parameters
$query_string = $_SERVER['QUERY_STRING'];
// The $base variable stores the complete URL to our page, including the current page arg
// if in the admin, your base should be the admin URL + your page
$base = admin_url('admin.php') . '?' . remove_query_arg('p', $query_string) . '%_%';
?>
<form method="get" action="<?php echo admin_url('admin.php');?>">
<p class="search-box">
<label class="screen-reader-text" for="user-search-input">Search Users:</label>
<input type="search" id="user-search-input" name="s" value="">
<input type="hidden" id="user-search-input" name="page" value="bespoke_registered_agent">
<input type="submit" id="search-submit" class="button" value="Search Users">
</p>
<table class="wp-list-table widefat fixed striped users">
<thead>
<tr>
<th scope="col" id="username" class="manage-column column-name">Username</th>
<th scope="col" id="email" class="manage-column column-email">Email</th>
</tr>
</thead>
<tbody id="the-list" data-wp-lists="list:user">
<?php if(!empty($all_agents)){
foreach($all_agents as $key => $agent){
$username = $agent->user_login;
$useremail = $agent->user_email;
$agentid = $agent->ID;
$profile_url = get_user_meta($agentid,'profile_picture',true);
if(empty($profile_url)){
$profile_url = BHD_GET_DIR_URL.'assets/images/no-photo-available-icon.jpg';
}
?>
<tr id="user-<?php echo $agentid; ?>">
<td class="username column-username has-row-actions column-primary" data-colname="Username">
<img alt="" src="https://stackoverflow.com/questions/40802138/<?php echo $profile_url;?>" class="avatar avatar-32 photo" height="32" width="32">
<strong>
</strong><br>
</td>
<td class="email column-email" data-colname="Email"><a href="mailto:<?php echo $useremail?>"><?php echo $useremail;?></a></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr class="no-items"><td class="colspanchange" colspan="3">No users found.</td></tr>
<?php }?>
</tbody>
<tfoot>
<tr>
<th scope="col" class="manage-column column-name">Username</th>
<th scope="col" class="manage-column column-email">Email</th>
</tr>
</tfoot>
</table>
</form>
<?php // if on the front end, your base is the current page
//$base = get_permalink( get_the_ID() ) . '?' . remove_query_arg('p', $query_string) . '%_%';
echo '<div id="pagination" class="tablenav-pages">';
echo paginate_links( array(
'base' => $base, // the base URL, including query arg
'format' => '&p=%#%', // this defines the query parameter that will be used, in this case "p"
'prev_text' => __('« Previous'), // text for previous page
'next_text' => __('Next »'), // text for next page
'total' => $total_pages, // the total number of pages we have
'current' => $page, // the current page
'end_size' => 1,
'mid_size' => 5,
));
echo '</div>';
?>
solved Listing , pagination and search custom user data by role in wordpress