wp_count_posts()WP 2.5.0

Gets the number of posts of any post type and any status (publish, draft).

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.

1 time — 0.00051 sec (slow) | 50000 times — 0.11 sec (very fast) | PHP 7.3.12, WP 5.4
Hooks from the function

Return

stdClass. An object containing the number of posts for each status, or an empty object if the post type does not exist.

Usage

$counts = wp_count_posts( $type, $perm );
$type(string)
The name of the post type whose number of posts you want to count.
Default: 'post'
$perm(string)
To add private posts of current authorized user to the counts, set this parameter to readable.
Default: ''

Examples

1

#1 Number of published posts

To get the number of published posts use this code:

$count_posts = wp_count_posts();
$published_posts = $count_posts->publish;

If you do not need PHP4 support, you can use such a shortened entry:

$published_posts = wp_count_posts()->publish;
0

#2 Consider drafts

To calculate how many drafts we have in the database use this code (for PHP5+):

$draft_posts = wp_count_posts()->draft;
0

#3 Consider other types of posts

To count how many published posts of custom post type we have, for example, new_post_type, use this code:

$published_posts = wp_count_posts('new_post_type')->publish;
-1

#4 Basic usage

Returns a data object about the number of posts of each status. You can use the var_dump() function to see what the object contains.

$count_posts = wp_count_posts();

// в итоге получим что-то такое:
/*
stdClass Object
(
	[publish] => 60
	[future]  => 0
	[draft]   => 9
	[pending] => 3
	[private] => 0
	[trash]   => 0
	[auto-draft] => 3
	[inherit]    => 0
)
*/

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 2.5.0 Introduced.

wp_count_posts() code WP 6.5.3

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

	$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";

	if ( 'readable' === $perm && is_user_logged_in() ) {
		$post_type_object = get_post_type_object( $type );
		if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
			$query .= $wpdb->prepare(
				" AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",
				get_current_user_id()
			);
		}
	}

	$query .= ' GROUP BY post_status';

	$results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), 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 );
}