wppaste
WordPress

WP_Posts_List_Table::prepare_items()

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

Compatibility

WordPress
core
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Performance profile

How much work a call to WP_Posts_List_Table::prepare_items() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
54–109

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 130.

Plugin surface
1 hook

Third-party callbacks on 'edit_posts_per_page' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()one call below WP_Posts_List_Table::prepare_items()
  • cacheobject cachewp_cache_get()one call below WP_Posts_List_Table::prepare_items()

Further down the call graph this can also reach serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 12 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Posts_List_Table::prepare_items() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($value)54–62get_user_setting(), wp_edit_posts_query(), is_post_type_hierarchical(), ->set_hierarchical_display(), ->get_items_per_page(), apply_filters(), total_items()
!empty($value)60–69set_user_setting(), wp_edit_posts_query(), is_post_type_hierarchical(), ->set_hierarchical_display(), ->get_items_per_page(), apply_filters(), total_items()
empty($value) && !$wp_query60–66get_user_setting(), wp_edit_posts_query(), is_post_type_hierarchical(), ->set_hierarchical_display(), ->get_items_per_page(), apply_filters(), ->get_pagenum(), total_items()
!empty($value) && !$wp_query66–73set_user_setting(), wp_edit_posts_query(), is_post_type_hierarchical(), ->set_hierarchical_display(), ->get_items_per_page(), apply_filters(), ->get_pagenum(), total_items()
empty($value) && !$wp_query && isset($value)74–85get_user_setting(), wp_edit_posts_query(), is_post_type_hierarchical(), ->set_hierarchical_display(), ->get_items_per_page(), apply_filters(), ->get_pagenum(), wp_count_posts(), total_items()
!empty($value) && !$wp_query && isset($value)80–92set_user_setting(), wp_edit_posts_query(), is_post_type_hierarchical(), ->set_hierarchical_display(), ->get_items_per_page(), apply_filters(), ->get_pagenum(), wp_count_posts(), total_items()
empty($value) && !$wp_query && !isset($value)80–95get_user_setting(), wp_edit_posts_query(), is_post_type_hierarchical(), ->set_hierarchical_display(), ->get_items_per_page(), apply_filters(), ->get_pagenum(), wp_count_posts(), array_sum(), get_post_stati(), total_items()
empty($value) && !$wp_query && isset($value)81–95get_user_setting(), wp_edit_posts_query(), is_post_type_hierarchical(), ->set_hierarchical_display(), ->get_items_per_page(), apply_filters(), ->get_pagenum(), wp_count_posts(), get_current_user_id(), total_items()
!empty($value) && !$wp_query && !isset($value)86–102set_user_setting(), wp_edit_posts_query(), is_post_type_hierarchical(), ->set_hierarchical_display(), ->get_items_per_page(), apply_filters(), ->get_pagenum(), wp_count_posts(), array_sum(), get_post_stati(), total_items()
!empty($value) && !$wp_query && isset($value)87–102set_user_setting(), wp_edit_posts_query(), is_post_type_hierarchical(), ->set_hierarchical_display(), ->get_items_per_page(), apply_filters(), ->get_pagenum(), wp_count_posts(), get_current_user_id(), total_items()
empty($value) && !$wp_query && isset($value)87–102get_user_setting(), wp_edit_posts_query(), is_post_type_hierarchical(), ->set_hierarchical_display(), ->get_items_per_page(), apply_filters(), ->get_pagenum(), wp_count_posts(), get_current_user_id(), array_sum(), get_post_stati(), total_items()
!empty($value) && !$wp_query && isset($value)93–109set_user_setting(), wp_edit_posts_query(), is_post_type_hierarchical(), ->set_hierarchical_display(), ->get_items_per_page(), apply_filters(), ->get_pagenum(), wp_count_posts(), get_current_user_id(), array_sum(), get_post_stati(), total_items()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev13054–10915
8.513054–10915
8.413054–109153 fewer instructions than PHP 8.3
8.313354–11215
8.213354–11215
8.113354–11215
7.413354–11215

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while WP_Posts_List_Table::prepare_items() runs, in this order:

  1. apply_filters( edit_posts_per_page )filterline 176 (+22 into the body)

    Filters the number of posts displayed per page when specifically listing "posts".

Uses · 12

Source code

	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,			)		);	}

Changelog

Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-admin/includes/class-wp-posts-list-table.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.