Custom model pagination bug

Support MB Custom Table Custom model pagination bug

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #31210
    wgstjfwgstjf
    Participant

    Hi guys,

    We've discovered a bug in the custom model pagination on the List Table view. It does not take into account the $where variable so always returns the total number of entries in the table

    The function determining the Total Items (located in src/Model/ListTable.php) is as follows:

    
    private function get_total_items() {
        global $wpdb;
        return $wpdb->get_var( "SELECT COUNT(*) FROM $this->table" );
    }
    

    The code needs changing as follows:

    
    public function prepare_items() {
        global $wpdb;
    
        $this->_column_headers = $this->get_column_info();
    
        $per_page = $this->get_items_per_page( "{$this->model->name}_per_page", 20 );
        $page     = $this->get_pagenum();
    
        $where = apply_filters( "mbct_{$this->model->name}_query_where", '' );
        $order = apply_filters( "mbct_{$this->model->name}_query_order", '' );
    
        $this->set_pagination_args( [
            'total_items' => $this->get_total_items($where),
            'per_page'    => $per_page,
            ] );  // Moved after $where is defined so that it can be passed to get_total_items()
    
        $limit  = "LIMIT $per_page";
        $offset = ' OFFSET ' . ( $page - 1 ) * $per_page;
        $sql    = "SELECT * FROM $this->table $where $order $limit $offset";
    
        $this->items = $wpdb->get_results( $sql, 'ARRAY_A' );
    }
    
    private function get_total_items($where) {
        global $wpdb;
        return $wpdb->get_var( "SELECT COUNT(*) FROM $this->table $where" ); // Added $where to query
    }
    

    Hopefully this fix can be included in the next update as well as the other items mentioned here: https://support.metabox.io/topic/custom-model-needs-additional-hooks-please/

    Cheers,

    Will

    #31229
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Thanks for your feedback.

    I will collect and forward them to the development team to improve the extension.

    #31232
    wgstjfwgstjf
    Participant

    THanks Long 🙂

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.