Created
September 14, 2017 08:13
-
-
Save alokstha1/2e8f7ed6517917aa345fc6f1f65f1fbd to your computer and use it in GitHub Desktop.
WordPress custom pagination with $wpdb->get_results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$items_per_page = 2; | |
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1; | |
$offset = ( $page * $items_per_page ) - $items_per_page; | |
$query = 'SELECT * FROM '.$table_name; | |
$total_query = "SELECT COUNT(1) FROM (${query}) AS combined_table"; | |
$total = $wpdb->get_var( $total_query ); | |
$results = $wpdb->get_results( $query.' ORDER BY id DESC LIMIT '. $offset.', '. $items_per_page, OBJECT ); | |
/* | |
* | |
* Here goes the loop | |
* | |
***/ | |
echo paginate_links( array( | |
'base' => add_query_arg( 'cpage', '%#%' ), | |
'format' => '', | |
'prev_text' => __('«'), | |
'next_text' => __('»'), | |
'total' => ceil($total / $items_per_page), | |
'current' => $page | |
)); |
Thanks man!
You are welcome!
4 years later and still works like a charm. Thanks!
4 years later and still works like a charm. Thanks!
@BlogSafe Glad it helped.
Great hack.
Thanks!
Thanks!
thanks good!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks man!