Developing a plugin in WordPress and getting well but stuck on pagination for the plugin page. Here is my code downloaded from internet ( got reference from here )
$items = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->review_media GROUP BY post_id")); // number of total rows in the database
// tested and got results as commented
print_r($items); // say this is outputs value 2
echo $rm_options[‘list_per_page’]; // this is my option set with value 1
if($items > 0) {
$p = new pagination;
$p->items($items);
$p->limit(empty($rm_options['list_per_page']) ? 20 : $rm_options['list_per_page']); // Limit entries per page
$p->target("admin.php?page=moderate.php");
$p->currentPage($_GET[$p->paging]); // Gets and validates the current page
$p->calculate(); // Calculates what to show
$p->parameterName('paging');
$p->adjacents(1); //No. of page away from the current page
if(!isset($_GET['paging'])) {
$p->page = 1;
} else {
$p->page = $_GET['paging'];
}
//Query for limit paging
$limit = "LIMIT " . ($p->page - 1) * $p->limit . ", " . $p->limit;
} else {
echo "No Record Found";
}
When I don’t group my query by post_id it is working fine but as soon as I grouped its behaving weird. It is creating a pagination links and getting blank page. I think the reason is grouping the row. But don’t know how to solve this.
Here is my table screenshot
Thanks a lot for your help…
If you use WordPress
WP_List_Table
class, it will take care of the pagination and provide a default table experience for the user.This tutorial covers it all, the pagination part is:
And here’s the full plugin shown in the tutorial. And in this other plugin you can see
WP_List_Table
in action using database data.