Hi, I currently have a wp_list_table up and running however it is showing all posts including, posts, pages etc. I was hoping to filter out results so it only shows pages or it only shows posts? The exact same way wordpress currently does it on the posts and pages.
if( is_admin() && !class_exists( 'WP_List_Table' ) )
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
class blav_wp_table extends WP_List_Table
{
private $order;
private $orderby;
private $posts_per_page = 10;
public function __construct()
{
parent :: __construct( array(
'singular' => 'table example',
'plural' => 'table examples',
'ajax' => true
) );
$this->set_order();
$this->set_orderby();
$this->prepare_items();
$this->display();
}
private function get_sql_results()
{
global $wpdb;
$args = array( 'ID', 'post_title', 'post_date', 'post_content', 'post_type' );
$sql_select = implode( ', ', $args );
$sql_results = $wpdb->get_results("
SELECT $sql_select
FROM $wpdb->posts
WHERE post_status = 'publish'
ORDER BY $this->orderby $this->order "
);
return $sql_results;
}
public function set_order()
{
$order = 'DESC';
if ( isset( $_GET['order'] ) AND $_GET['order'] )
$order = $_GET['order'];
$this->order = esc_sql( $order );
}
public function set_orderby()
{
$orderby = 'post_date';
if ( isset( $_GET['orderby'] ) AND $_GET['orderby'] )
$orderby = $_GET['orderby'];
$this->orderby = esc_sql( $orderby );
}
public function ajax_user_can()
{
return current_user_can( 'edit_posts' );
}
public function no_items()
{
_e( 'No posts found.' );
}
public function get_views()
{
return array();
}
public function get_columns()
{
$columns = array(
'post_title' => __( 'Page Name' ),
'post_date' => __( 'Date Created' )
);
return $columns;
}
public function get_sortable_columns()
{
$sortable = array(
'ID' => array( 'ID', true ),
'post_title' => array( 'post_title', true ),
'post_date' => array( 'post_date', true )
);
return $sortable;
}
public function prepare_items()
{
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array(
$columns,
$hidden,
$sortable
);
$posts = $this->get_sql_results();
empty( $posts ) AND $posts = array();
# >>>> Pagination
$per_page = $this->posts_per_page;
$current_page = $this->get_pagenum();
$total_items = count( $posts );
$this->set_pagination_args( array (
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil( $total_items / $per_page )
) );
$last_post = $current_page * $per_page;
$first_post = $last_post - $per_page + 1;
$last_post > $total_items AND $last_post = $total_items;
$range = array_flip( range( $first_post - 1, $last_post - 1, 1 ) );
$posts_array = array_intersect_key( $posts, $range );
# <<<< Pagination
$permalink = __( 'Edit:' );
foreach ( $posts_array as $key => $post )
{
$link = get_edit_post_link( $post->ID );
$no_title = __( 'No title set' );
$title = ! $post->post_title ? "{$no_title}" : $post->post_title;
$posts[ $key ]->post_title = "<a title='{$permalink} {$title}' href='{$link}'>{$title}</a>";
}
$this->items = $posts_array;
}
public function column_default( $item, $column_name )
{
return $item->$column_name;
}
public function display_tablenav( $which ) {
?>
<div class="tablenav <?php echo esc_attr( $which ); ?>">
<!--
<div class="alignleft actions">
<?php # $this->bulk_actions( $which ); ?>
</div>
-->
<?php
$this->extra_tablenav( $which );
$this->pagination( $which );
?>
<br class="clear" />
</div>
<?php
}
public function extra_tablenav( $which )
{
global $wp_meta_boxes;
$views = $this->get_views();
if ( empty( $views ) )
return;
$this->views();
}
}