Pagination of custom column added in user table when sorting WordPress

Custom column named company is added in user table in WordPress. Also sorting functionality of that column is made. However pagination doesn’t show up when the table is sorted with that custom column.

if ('company' == $query->query_vars['orderby']) {
    $query->query_fields = 'u.*,um.*,c1.*';
    $query->query_from = 'FROM '. $wpdb->prefix.'users as u JOIN '. $wpdb->prefix . 'usermeta as um ON u.ID=um.user_id JOIN ' . $wpdb->prefix . 'company as c1 ON um.meta_value=c1.company_id';
    $query->query_orderby = ' ORDER BY company_name ' . ($query->query_vars["order"] == "ASC" ? "asc " : "desc "); 
    $query->query_limit='LIMIT 20';
}

Also, if we hit the url of page 2 like this

Read More
http://example.com/wp-admin/users.php?orderby=company&order=asc&paged=2

it will redirect to

http://example.com/wp-admin/users.php?orderby=company&order=asc&paged=1

Related posts

1 comment

  1. I got it

     if ('company' == $query->query_vars['orderby']) {
    $limit =20;
            if(isset($_GET['paged'])){
                $page = $_GET['paged'];
                $offset = ($page - 1)  * $limit;
                $start = $offset + 1;
            }else{
                $start = 1;
            }
            if($_GET['paged'] == 1){
                $start = 1;
            }
            $query->query_fields = 'u.ID';
            $query->query_from = 'FROM '. $wpdb->prefix.'users as u JOIN '. $wpdb->prefix . 'usermeta as um ON u.ID=um.user_id JOIN ' . $wpdb->prefix . 'company as c1 ON um.meta_value=c1.company_id';
            $query->query_orderby = ' ORDER BY company_name ' . ($query->query_vars["order"] == "ASC" ? "asc " : "desc "); 
          $query->query_limit="LIMIT $start, $limit";
        }
    

    Select only user id not all.

       $query->query_fields = 'u.ID';
    

    instead of

     $query->query_fields = 'u.*,um.*,c1.*';
    

    and change $query->query_limit=20;

    to $query->query_limit="LIMIT $start, $limit";
    

Comments are closed.