Admin_Apple_News_List_Table::prepare_items PHP Method

prepare_items() public method

Prepare items for the table.
public prepare_items ( )
    public function prepare_items()
    {
        // Set column headers. It expects an array of columns, and as second
        // argument an array of hidden columns, which in this case is empty.
        $columns = $this->get_columns();
        $this->_column_headers = array($columns, array(), array());
        // Build the default args for the query
        $current_page = $this->get_pagenum();
        $args = array('post_type' => $this->settings->get('post_types'), 'post_status' => 'publish', 'posts_per_page' => $this->per_page, 'offset' => ($current_page - 1) * $this->per_page, 'orderby' => 'ID', 'order' => 'DESC');
        // Add the publish status filter if set
        $publish_status = $this->get_publish_status_filter();
        if (!empty($publish_status)) {
            switch ($publish_status) {
                case 'published':
                    $args['meta_query'] = array(array('key' => 'apple_news_api_id', 'compare' => '!=', 'value' => ''));
                    break;
                case 'not_published':
                    $args['meta_query'] = array('relation' => 'AND', array('relation' => 'OR', array('key' => 'apple_news_api_id', 'compare' => 'NOT EXISTS'), array('key' => 'apple_news_api_id', 'compare' => '=', 'value' => '')), array('key' => 'apple_news_api_deleted', 'compare' => 'NOT EXISTS'));
                    break;
                case 'deleted':
                    $args['meta_query'] = array(array('key' => 'apple_news_api_deleted', 'compare' => 'EXISTS'));
                    break;
                case 'pending':
                    $args['meta_query'] = array(array('key' => 'apple_news_api_pending', 'compare' => 'EXISTS'));
                    break;
            }
        }
        // Add the date filters if set
        $date_from = $this->get_date_from_filter();
        $date_to = $this->get_date_to_filter();
        if (!empty($date_from) || !empty($date_to)) {
            $args['date_query'] = array(array('inclusive' => true));
            if (!empty($date_from)) {
                $args['date_query'][0]['after'] = $date_from;
            }
            if (!empty($date_to)) {
                $args['date_query'][0]['before'] = $date_to;
            }
        }
        // Add the search filter if set
        $search = $this->get_search_filter();
        if (!empty($search)) {
            $args['s'] = $search;
        }
        // Data fetch
        $query = new WP_Query(apply_filters('apple_news_export_table_get_posts_args', $args));
        // Set data
        $this->items = $query->posts;
        $total_items = $query->found_posts;
        $this->set_pagination_args(apply_filters('apple_news_export_table_pagination_args', array('total_items' => $total_items, 'per_page' => $this->per_page, 'total_pages' => ceil($total_items / $this->per_page))));
    }

Usage Example

 /**
  * Shows a post from the list table.
  *
  * @access private
  */
 private function show_post_list_action()
 {
     $table = new Admin_Apple_News_List_Table($this->settings);
     $table->prepare_items();
     include plugin_dir_path(__FILE__) . 'partials/page_index.php';
 }