wp_count_posts( string $type = 'post', string $perm = '' ): stdClass
- Since
- 2.5.0
- Source
wp-includes/post.php:3464
Description
This function provides an efficient method of finding the amount of post's type a blog has. Another method is to count the amount of items in get_posts(), but that method has a lot of overhead with doing so. Therefore, when developing for 2.5+, use this function instead.
The $perm parameter checks for 'readable' value and if the user can read private posts, it will display that for the user that is signed in.
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
$typestringoptional- Post type to retrieve count. Default 'post'.Default:
'post' $permstringoptional- 'readable' or empty. Default empty.Default:
''
Return value
stdClass- An object containing the number of posts for each status, or an empty object if the post type does not exist.
Performance profile
How much work a call to wp_count_posts() 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
- Scaling
- Scales with input
- Instructions
- 10–84
- Plugin surface
- 1 hook
- Called by
- 4
Reaches the database via ->get_results().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 114.
Third-party callbacks on 'wp_count_posts' run inside this call, and their cost is not bounded by anything here.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_get()called directly - hookthird-party callbacks
apply_filters()called directly - sqldatabase query
->get_results()called directly
Further down the call graph this can also reach query. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_count_posts() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!post_type_exists() | 10 | post_type_exists() |
post_type_exists() && $counts !== false | 30–31 | post_type_exists(), _count_posts_cache_key(), wp_cache_get(), get_post_stati(), apply_filters() |
post_type_exists() && $counts === false && $perm !== "readable" | 61–62 | post_type_exists(), _count_posts_cache_key(), wp_cache_get(), ->prepare(), ->get_results(), get_post_stati(), array_fill_keys(), wp_cache_set(), apply_filters() |
post_type_exists() && $counts === false && $perm === "readable" && !is_user_logged_in() | 64–65 | post_type_exists(), _count_posts_cache_key(), wp_cache_get(), is_user_logged_in(), ->prepare(), ->get_results(), get_post_stati(), array_fill_keys(), wp_cache_set(), apply_filters() |
post_type_exists() && $counts === false && $perm === "readable" && is_user_logged_in() && current_user_can() | 74–75 | post_type_exists(), _count_posts_cache_key(), wp_cache_get(), is_user_logged_in(), get_post_type_object(), current_user_can(), ->prepare(), ->get_results(), get_post_stati(), array_fill_keys(), wp_cache_set(), apply_filters() |
post_type_exists() && $counts === false && $perm === "readable" && is_user_logged_in() && !current_user_can() | 83–84 | post_type_exists(), _count_posts_cache_key(), wp_cache_get(), is_user_logged_in(), get_post_type_object(), current_user_can(), get_current_user_id(), ->prepare(), ->get_results(), get_post_stati(), array_fill_keys(), wp_cache_set(), apply_filters() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 114 instructions, 10–84 executed per call, 10 branches. The work does not change between versions.
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_count_posts() runs, in this order:
- apply_filters( wp_count_posts )filterline 3483 (+19 into the body)
Filters the post counts by status for the current post type.
- apply_filters( wp_count_posts )filterline 3540 (+76 into the body)
Filters the post counts by status for the current post type.
Uses · 11
- post_type_exists()Determines whether a post type is registered.
- _count_posts_cache_key()Returns the cache key for wp_count_posts() based on the passed arguments.
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- get_post_stati()Gets a list of post statuses.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_user_logged_in()Determines whether the current visitor is a logged in user.
- current_user_can()Returns whether the current user has the specified capability.
- get_post_type_object()Retrieves a post type object by name.
- get_current_user_id()Gets the current user's ID.
- wp_cache_set()Saves the data to the cache.
- stdClass::__construct()
Used by · 4
- WP_Posts_List_Table::get_views()
- WP_Posts_List_Table::prepare_items()
- get_available_post_statuses()Returns all the possible statuses for a post type.
- wp_dashboard_right_now()Dashboard widget that displays some basic stats about the site.
Source code
function wp_count_posts( $type = 'post', $perm = '' ) { global $wpdb; if ( ! post_type_exists( $type ) ) { return new stdClass(); } $cache_key = _count_posts_cache_key( $type, $perm ); $counts = wp_cache_get( $cache_key, 'counts' ); if ( false !== $counts ) { // We may have cached this before every status was registered. foreach ( get_post_stati() as $status ) { if ( ! isset( $counts->{$status} ) ) { $counts->{$status} = 0; } } /** This filter is documented in wp-includes/post.php */ return apply_filters( 'wp_count_posts', $counts, $type, $perm ); } if ( 'readable' === $perm && is_user_logged_in() && ! current_user_can( get_post_type_object( $type )->cap->read_private_posts ) ) { // Optimized query uses subqueries which can leverage DB indexes for better performance. See #61097. $query = " SELECT post_status, COUNT(*) AS num_posts FROM ( SELECT post_status FROM {$wpdb->posts} WHERE post_type = %s AND post_status != 'private' UNION ALL SELECT post_status FROM {$wpdb->posts} WHERE post_type = %s AND post_status = 'private' AND post_author = %d ) AS filtered_posts "; $args = array( $type, $type, get_current_user_id() ); } else { $query = " SELECT post_status, COUNT(*) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s "; $args = array( $type ); } $query .= ' GROUP BY post_status'; $results = (array) $wpdb->get_results( $wpdb->prepare( $query, ...$args ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Placeholders are used in the string contained in the variable. ARRAY_A ); $counts = array_fill_keys( get_post_stati(), 0 ); foreach ( $results as $row ) { $counts[ $row['post_status'] ] = $row['num_posts']; } $counts = (object) $counts; wp_cache_set( $cache_key, $counts, 'counts' ); /** * Filters the post counts by status for the current post type. * * @since 3.7.0 * * @param stdClass $counts An object containing the current post_type's post * counts by status. * @param string $type Post type. * @param string $perm The permission to determine if the posts are 'readable' * by the current user. */ return apply_filters( 'wp_count_posts', $counts, $type, $perm );}Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-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.