WordPress at a glance

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.

  • Global. wpdb. $wpdb WordPress database abstraction object.
✈ 1 time = 0.000496s = fast | 50000 times = 0.23s = very fast PHP 7.0.8, WP 4.7
Hooks from function:
Return

Object. Number of posts for each status.

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 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
)
*/

#2 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;

#3 Consider drafts

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

$draft_posts = wp_count_posts()->draft;

#4 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;

Code of wp_count_posts: wp-includes/post.php VER 4.9.8

<?php
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 ) {
		/** 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' );

	/**
	 * Modify returned post counts by status for the current post type.
	 *
	 * @since 3.7.0
	 *
	 * @param object $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 );
}

Related Functions

From tag: counts (counting of any)

More from category: Queries

No comments
    Hello, !     Log In . Register