WordPress: Having trouble in pagination for plugin settings page

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

Read More
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

enter image description here

Thanks a lot for your help…

Related posts

Leave a Reply

1 comment

  1. 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:

    function prepare_items() {
      [...]
      $per_page = 5;
      $current_page = $this->get_pagenum();
      $total_items = count($this->example_data);
    
      // only necessary because we have sample data
      $this->found_data = array_slice($this->example_data,(($current_page-1)*$per_page),$per_page);
    
      $this->set_pagination_args( array(
        'total_items' => $total_items, //WE have to calculate the total number of items
        'per_page'    => $per_page     //WE have to determine how many items to show on a page
      ) );
      $this->items = $this->found_data;
    }
    

    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.