Main menu
WalkswithMeWordPressWordPress Pagination for custom tables

WordPress Pagination for custom tables

WordPress pagination for custom tables are important in WordPress CMS. When we add more features and options to our WordPress site its not just like a blog so this case we required many custom tables and queries.

How to create a nice and simple WordPress Pagination for your custom tables data listing. Its easy and quick than you think, basically I’m also faced such a situation I have a listing of my extensions, Its not a post or page , I’m managing that with separate table so pagination will create a complication. I checked many extensions but all of them are managing only post or pages. So I found something useful for you guys too.

wordpress pagination for custom tables

WordPress pagination for custom tables

ok lets check how to manage WordPress pagination for custom tables, Simply add the following codes in your template  functions.php or add the scripts where ever you need the pagination.


$customPagHTML     = "";
$query             = "SELECT * FROM custom_table";
$total_query     = "SELECT COUNT(1) FROM (${query}) AS combined_table";
$total             = $wpdb->get_var( $total_query );
$items_per_page = 4;
$page             = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset         = ( $page * $items_per_page ) - $items_per_page;
$result         = $wpdb->get_results( $query . " ORDER BY field DESC LIMIT ${offset}, ${items_per_page}" );
$totalPage         = ceil($total / $items_per_page);

Then above script have “custom_table” that you need to replace with your table name also you have option to add ORDER BY or GROUP BY etc. Then just iterate the “$result” that have your query result.

The for a nice pagination simply use below codes .


if($totalPage > 1){
$customPagHTML     =  '<div><span>Page '.$page.' of '.$totalPage.'</span>'.paginate_links( array(
'base' => add_query_arg( 'cpage', '%#%' ),
'format' => '',
'prev_text' => __('&laquo;'),
'next_text' => __('&raquo;'),
'total' => $totalPage,
'current' => $page
)).'</div>';
}

Then simply output the $customPagHTML variable where ever you need the pagination like below. Using this way you can show the pagination on top of the page and bottom of the page.


echo $customPagHTML;

I hope this article will helps you guys to create a simple and quick pagination for your WordPress custom table data. If you have any better methods feel free to share here..

21 thoughts on “WordPress Pagination for custom tables

  1. If you have a really big table – few hundred thousands rows, then you can speed your query up by selecting not all the fields (don’t use *), but only one unique field.
    $query = “SELECT id FROM `your_table`”;
    $total_query = “SELECT COUNT(1) FROM (${query}) AS combined_table”;

    Thanks for sharing.

  2. Thanks a lot! You saved my life, I was banging my head to the wall. I get stuck in the piece of code and not getting any result. You are awesome. Many many thanks.

  3. This is the best and yet the simplest code to paginate results from custom wp database table, from now on this site is at my favourite sites.

  4. Thank you so much for this tutorial. Very helpful and I learned a lot since I am a self learner in PHP and WordPress. I just have one question though. How can I add in the pagination link of an anchor?
    For example paginationation link is http://yoursite.com/author/?cpage=2&#feedbacks

    The #feedbacks above is my anchor so that after loading the next page, the page will automatically go where #feedbacks is put. Thank you and I hope some will be able to help me here.

  5. Hi I am facing an issue . Initially page links where showing correctly , but when i click on any page number, it will load the correct page but after that all the pages are pointing to the same page.
    Plz help

  6. Thanks for this. Your code did all the heavy lifting for me when I needed to paginate records in my WordPress plugin.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

FacebookTwitterGoogle+RSS