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.
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' => __('«'),
'next_text' => __('»'),
'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”
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.
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.
you’re welcome 🙂
it save my time , thanks
May God Save Your heart And soul ! 🙂
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.
Hi admin,
Thank you for paging tip with custom tables. My issue was solved!
Anna,
hi freind can you share here the snip code that solve your issue for the custom table pagination
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.
Sorry for the delay,
if the pagination links all need to append with same #feedback then simply use jquery to append that to you links
My pagination looks like this http://postimg.org/image/4z7w1ev1v/
Please help
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
great, it works fine
Thank you so much for posting..its very usefull for me..
Thank you its working nice…
Thanks for this it works well.
A bit more CSS styling and I will have something that looks great.
Thanks once again. A little custom CSS and it now looks great imho. Take a look at http://pics.turnspain.com/wp-content/uploads/2014/11/pag.jpg
very useful…thnq 🙂
thanks.
very usefull article.
Thanks for this. Your code did all the heavy lifting for me when I needed to paginate records in my WordPress plugin.
Thank you.