Add Pagination in admin for Listing in WordPress

In this tutorial I will explain how to add pagination in admin for listing in WordPress. In many WordPress application,  we need to deal with list of records in custom plug-in. We need to display all the records but as the records grow with time it slow down the system if we fetch and display all of them.

So we have to organize the records by limiting the number of records to show per page.  This tutorial will help you to add a pagination in admin for listing.

First get the current page number with following code:

$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;

Now find the total number of records available with below code:

$limit = 10; // number of rows in page
$offset = ( $pagenum - 1 ) * $limit;
$total = $wpdb->get_var( "SELECT COUNT(`id`) FROM {$wpdb->prefix}table_name" );
$num_of_pages = ceil( $total / $limit );

Give this limit and offset in query and get records for the page:

$records = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}table_name LIMIT $offset, $limit" );

Now add below code to display the paging where you want in the page.

$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '«', 'domain' ),
    'next_text' => __( '»', 'domain' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '
<div class="tablenav">
<div class="tablenav-pages">' . $page_links . '</div>


You will see a paging numbers where you added the code above like the below image.

pagination in admin
pagination in admin

Hope this tutorial will help you to add pagination in admin for listing in WordPress.