WP_Posts_List_Table::prepare_items() WordPress Method

The WP_Posts_List_Table::prepare_items() method is used to prepare the data for display in the WordPress posts list table. This includes sorting the data, paginating the data, and setting the correct headers.

WP_Posts_List_Table::prepare_items() #


Source

File: wp-admin/includes/class-wp-posts-list-table.php

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
public function prepare_items() {
    global $mode, $avail_post_stati, $wp_query, $per_page;
 
    if ( ! empty( $_REQUEST['mode'] ) ) {
        $mode = 'excerpt' === $_REQUEST['mode'] ? 'excerpt' : 'list';
        set_user_setting( 'posts_list_mode', $mode );
    } else {
        $mode = get_user_setting( 'posts_list_mode', 'list' );
    }
 
    // Is going to call wp().
    $avail_post_stati = wp_edit_posts_query();
 
    $this->set_hierarchical_display(
        is_post_type_hierarchical( $this->screen->post_type )
        && 'menu_order title' === $wp_query->query['orderby']
    );
 
    $post_type = $this->screen->post_type;
    $per_page  = $this->get_items_per_page( 'edit_' . $post_type . '_per_page' );
 
    /** This filter is documented in wp-admin/includes/post.php */
    $per_page = apply_filters( 'edit_posts_per_page', $per_page, $post_type );
 
    if ( $this->hierarchical_display ) {
        $total_items = $wp_query->post_count;
    } elseif ( $wp_query->found_posts || $this->get_pagenum() === 1 ) {
        $total_items = $wp_query->found_posts;
    } else {
        $post_counts = (array) wp_count_posts( $post_type, 'readable' );
 
        if ( isset( $_REQUEST['post_status'] ) && in_array( $_REQUEST['post_status'], $avail_post_stati, true ) ) {
            $total_items = $post_counts[ $_REQUEST['post_status'] ];
        } elseif ( isset( $_REQUEST['show_sticky'] ) && $_REQUEST['show_sticky'] ) {
            $total_items = $this->sticky_posts_count;
        } elseif ( isset( $_GET['author'] ) && get_current_user_id() === (int) $_GET['author'] ) {
            $total_items = $this->user_posts_count;
        } else {
            $total_items = array_sum( $post_counts );
 
            // Subtract post types that are not included in the admin all list.
            foreach ( get_post_stati( array( 'show_in_admin_all_list' => false ) ) as $state ) {
                $total_items -= $post_counts[ $state ];
            }
        }
    }
 
    $this->is_trash = isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'];
 
    $this->set_pagination_args(
        array(
            'total_items' => $total_items,
            'per_page'    => $per_page,
        )
    );
}

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.