wp_edit_posts_query( array|false $q = false ): array
- Since
- 2.5.0
- Source
wp-admin/includes/post.php:1216
Compatibility
- WordPress
- since 2.5.0
- 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.
Parameters
$qarray|falseoptional- Array of query variables to use to build the query.
Defaults to the$_GETsuperglobal.Default:false
Return value
array
Performance profile
How much work a call to wp_edit_posts_query() 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
- Moderate
- Scaling
- Constant
- Instructions
- 82–125
- Plugin surface
- 2 hooks
- Called by
- 1
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 140.
Third-party callbacks on 'edit_{$post_type}_per_page', 'edit_posts_per_page' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()called directly - cacheobject cache
wp_cache_get()one call below wp_edit_posts_query() - serializeserialisation
maybe_unserialize()one call below wp_edit_posts_query()
Further down the call graph this can also reach 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 · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_edit_posts_query() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$q !== false && !isset($q) && empty($q) | 82–113 | get_post_stati(), get_available_post_statuses(), get_user_option(), apply_filters(), apply_filters(), compact(), is_post_type_hierarchical(), wp() |
$q !== false && !isset($q) && !empty($q) | 88–119 | get_post_stati(), get_available_post_statuses(), get_user_option(), apply_filters(), apply_filters(), compact(), is_post_type_hierarchical(), get_option(), wp() |
$q !== false && empty($q) | 88–119 | get_post_stati(), get_post_types(), get_available_post_statuses(), get_user_option(), apply_filters(), apply_filters(), compact(), is_post_type_hierarchical(), wp() |
$q !== false && !empty($q) | 94–125 | get_post_stati(), get_post_types(), get_available_post_statuses(), get_user_option(), apply_filters(), apply_filters(), compact(), is_post_type_hierarchical(), get_option(), wp() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 140 | 82–125 | 18 | |
| 8.5 | 140 | 82–125 | 18 | |
| 8.4 | 140 | 82–125 | 18 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 146 | 82–131 | 18 | |
| 8.2 | 146 | 82–131 | 18 | |
| 8.1 | 146 | 82–131 | 18 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 148 | 82–131 | 18 |
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 · 2
2 hooks fire while wp_edit_posts_query() runs, in this order:
- apply_filters( edit_{$post_type}_per_page )filterline 1279 (+63 into the body)
Filters the number of items per page to show for a specific 'per_page' type.
- apply_filters( edit_posts_per_page )filterline 1289 (+73 into the body)
Filters the number of posts displayed per page when specifically listing "posts".
Uses · 8
- get_post_stati()Gets a list of post statuses.
- get_post_types()Gets a list of all registered post type objects.
- get_available_post_statuses()Returns all the possible statuses for a post type.
- get_user_option()Retrieves user option that can be either per Site or per Network.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_post_type_hierarchical()Determines whether the post type is hierarchical.
- get_option()Retrieves an option value based on an option name.
- wp()Sets up the WordPress query.
Used by · 1
Source code
function wp_edit_posts_query( $q = false ) { if ( false === $q ) { $q = $_GET; } $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0; $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0; $post_statuses = get_post_stati(); if ( isset( $q['post_type'] ) && in_array( $q['post_type'], get_post_types(), true ) ) { $post_type = $q['post_type']; } else { $post_type = 'post'; } $avail_post_stati = get_available_post_statuses( $post_type ); $post_status = ''; $perm = ''; if ( isset( $q['post_status'] ) && in_array( $q['post_status'], $post_statuses, true ) ) { $post_status = $q['post_status']; $perm = 'readable'; } $orderby = ''; if ( isset( $q['orderby'] ) ) { $orderby = $q['orderby']; } elseif ( isset( $q['post_status'] ) && in_array( $q['post_status'], array( 'pending', 'draft' ), true ) ) { $orderby = 'modified'; } $order = ''; if ( isset( $q['order'] ) ) { $order = $q['order']; } elseif ( isset( $q['post_status'] ) && 'pending' === $q['post_status'] ) { $order = 'ASC'; } $per_page = "edit_{$post_type}_per_page"; $posts_per_page = (int) get_user_option( $per_page ); if ( empty( $posts_per_page ) || $posts_per_page < 1 ) { $posts_per_page = 20; } /** * Filters the number of items per page to show for a specific 'per_page' type. * * The dynamic portion of the hook name, `$post_type`, refers to the post type. * * Possible hook names include: * * - `edit_post_per_page` * - `edit_page_per_page` * - `edit_attachment_per_page` * * @since 3.0.0 * * @param int $posts_per_page Number of posts to display per page for the given post * type. Default 20. */ $posts_per_page = apply_filters( "edit_{$post_type}_per_page", $posts_per_page ); /** * Filters the number of posts displayed per page when specifically listing "posts". * * @since 2.8.0 * * @param int $posts_per_page Number of posts to be displayed. Default 20. * @param string $post_type The post type. */ $posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type ); $query = compact( 'post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page' ); // Hierarchical types require special args. if ( is_post_type_hierarchical( $post_type ) && empty( $orderby ) ) { $query['orderby'] = 'menu_order title';Changelog
Introduced in 2.5.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
array to string[].verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-admin/includes/post.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.