altering user_admin_account form I am working to change the standard display when pressing the "People" button.
Instead of showing "Username" I want it to show a custom field. For example "Last name".

I almost made it work, but the pager is not working. When I click page 2, the same info shows as page 1. Sorting is working though.

Here's the slightly changed code, which originated from the core user module.

&lt;?php
function user_custom_form_user_admin_account_alter(&amp;$form, &amp;$form_state, $form_id) {

  $header = array(
    'field_lastname_value' =&gt; array('data' =&gt; t('Username'), 'field' =&gt; 'dn.field_lastname_value', 'sort' =&gt; 'asc'),
    'status' =&gt; array('data' =&gt; t('Status'), 'field' =&gt; 'u.status'),
    'roles' =&gt; array('data' =&gt; t('Roles')),
    'member_for' =&gt; array('data' =&gt; t('Member for'), 'field' =&gt; 'u.created'),
    'access' =&gt; array('data' =&gt; t('Last access'), 'field' =&gt; 'u.access'),
    'operations' =&gt; array('data' =&gt; t('Operations')),
  );  

  $query = db_select('users', 'u');
  $query-&gt;condition('u.uid', 0, '&lt;&gt;');  
  $query-&gt;leftJoin('field_data_field_lastname', 'dn', 'u.uid = dn.entity_id');
  user_build_filter_query($query);

  $count_query = clone $query;
  $count_query-&gt;addExpression('COUNT(dn.entity_id)');    // or u.uid, does not make any diff.

  $query = $query-&gt;extend('PagerDefault')-&gt;extend('TableSort');
  $query
    -&gt;fields('u', array('uid', 'name', 'status', 'created', 'access'))
    -&gt;fields('dn', array('field_lastname_value'))
    -&gt;limit(20)
    -&gt;orderByHeader($header)
    -&gt;setCountQuery($count_query);
  $result = $query-&gt;execute();

...

    $account-&gt;name = $account-&gt;field_lastname_value;
    $options[$account-&gt;uid] = array(
      'field_lastname_value' =&gt; theme('username', array('account' =&gt; $account)),
      'status' =&gt;  $status[$account-&gt;status],
      'roles' =&gt; theme('item_list', array('items' =&gt; $users_roles)),
      'member_for' =&gt; format_interval(REQUEST_TIME - $account-&gt;created),
      'access' =&gt;  $account-&gt;access ? t('@time ago', array('@time' =&gt; format_interval(REQUEST_TIME - $account-&gt;access))) : t('never'),
      'operations' =&gt; array('data' =&gt; array('#type' =&gt; 'link', '#title' =&gt; t('edit'), '#href' =&gt; "user/$account-&gt;uid/edit", '#options' =&gt; array('query' =&gt; $destination))),
    );




      ...
}
        ?&gt;


Can anyone help? I have been struggling with this for hours. I did try changing the position of pagerDefault and TableSort to all kind of possible places like in the beginning before the join, after the join, tried with other joins then just left join, I'm stuck. Maybe I have to call SELECT FROM the custom field table and join the users table? But that's odd.
Any help is really appreciated.
I faced a similar issue while adding a new field 'Mail' in the 'user_admin_account' form. 

During my search I came across this post &amp; also this post 'https://drupal.org/node/1899708' which address the same issue. I got a solution for this as described below:


/**
 * Implements hook_form_FORMID_alter()
 * @param $form
 * @param $form_state
 * @param $form_id
 * @return $form 
 */
function CUSTOMMODULE_form_user_admin_account_alter(&$form, &$form_state, $form_id) {
  // Adding User Mail Field in the 'user_admin_account' form
  $mail_header = array(
    'mail' => array('data' => t('Mail'), 'field' => 'u.mail'),
  );
  // Placing Mail at 2nd position
  array_splice($form['accounts']['#header'], 1, 0, $mail_header);

  foreach($form['accounts']['#options'] as $key =>$value){
       $query = db_select('users', 'u')
        ->fields('u', array('mail'))
        ->condition('u.uid', $key)
        ->execute()
    ->fetchAssoc();

    array_unshift($form['accounts']['#options'][$key], $query['mail']);
  }

  $form['pager'] = array('#markup' => theme('pager'));

  return $form;
}


Although this post dates back in '11, but as i did not get any solution based on my search so considered to post this answer. Hope it helps.