class Item_List_Table extends WP_List_Table { function column_default( $item, $column_name ) { switch ( $column_name ) { case 'name': return $item['name']; case 'description': return $item['description']; case 'price': return $item['price']; default: return print_r( $item, true ); //Show the whole array for debugging purposes } } function prepare_items() { $data = array( array( 'name' => 'Item 1', 'description' => 'This is the first item', 'price' => '$10', ), array( 'name' => 'Item 2', 'description' => 'This is the second item', 'price' => '$20', ), ); $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); $this->_column_headers = array( $columns, $hidden, $sortable ); usort( $data, array( &$this, 'usort_reorder' ) ); $per_page = 10; $current_page = $this->get_pagenum(); $total_items = count( $data ); $this->set_pagination_args( array( 'total_items' => $total_items, 'per_page' => $per_page, ) ); $data = array_slice( $data, ( $current_page - 1 ) * $per_page, $per_page ); $this->items = $data; } function get_columns() { $columns = array( 'name' => 'Name', 'description' => 'Description', 'price' => 'Price', ); return $columns; } function get_sortable_columns() { $sortable_columns = array( 'name' => array( 'name', false ), 'price' => array( 'price', false ), ); return $sortable_columns; } function usort_reorder( $a, $b ) { $orderby = ( ! empty( $_REQUEST['orderby'] ) ) ? $_REQUEST['orderby'] : 'name'; $order = ( ! empty( $_REQUEST['order'] ) ) ? $_REQUEST['order'] : 'asc'; $result = strcmp( $a[$orderby], $b[$orderby] ); return ( $order === 'asc' ) ? $result : - $result; } } function my_item_list_table() { $item_table = new Item_List_Table(); $item_table->prepare_items(); $item_table->display(); }
class Post_List_Table extends WP_List_Table { function column_default( $item, $column_name ) { switch ( $column_name ) { case 'title': return '' . $item['post_title'] . ''; case 'author': return get_the_author_meta( 'user_nicename', $item['post_author'] ); case 'date': return get_the_date( '', $item['ID'] ); default: return print_r( $item, true ); //Show the whole array for debugging purposes } } function prepare_items() { $query = new WP_Query( array( 'post_type' => 'post', 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 10, 'paged' => $this->get_pagenum(), ) ); $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); $this->_column_headers = array( $columns, $hidden, $sortable ); $this->items = $query->get_posts(); $total_items = $query->found_posts; $per_page = $query->query_vars['posts_per_page']; $this->set_pagination_args( array( 'total_items' => $total_items, 'per_page' => $per_page, ) ); } function get_columns() { $columns = array( 'title' => 'Title', 'author' => 'Author', 'date' => 'Date', ); return $columns; } function get_sortable_columns() { $sortable_columns = array( 'title' => array( 'title', false ), 'author' => array( 'author', false ), 'date' => array( 'date', true ), ); return $sortable_columns; } } function my_post_list_table() { $post_table = new Post_List_Table(); $post_table->prepare_items(); $post_table->display(); }This example creates a table that displays a list of posts with columns for title, author, and date. The table is sortable by title, author, and date columns, and the number of posts displayed per page is set to 10. This code is part of the WordPress core package.