Codex tools: Log in
This function was introduced in WordPress Version 2.5, and outputs the number for each post status of a post type. You can also use
wp_count_posts() as a template_tag with the second parameter and include the private post status. By default, or if the user isn't logged in or is a guest of your site, then private post status post count will not be included.
This function will return an object with the post statuses as the properties. You should check for the property using
isset() PHP function, if you are wanting the value for the private post status. Not all post statuses will be part of the object.
<?php wp_count_posts( $type, $perm ); ?>
The default usage returns a count of the posts that are published. This will be an object, you can var_dump() the contents to debug the output.
<?php $count_posts = wp_count_posts(); ?>
To get the published status type, you would call the
wp_count_posts() function and then access the 'publish' property.
<?php $count_posts = wp_count_posts(); $published_posts = $count_posts->publish; ?>
If you are developing for PHP5 only, then you can use shorthand, if you only want to get one status. This will not work in PHP4 and if you want to maintain backwards compatibility, then you must use the above code.
<?php $published_posts = wp_count_posts()->publish; ?>
Counting drafts is handled the same way as the publish status.
<?php $count_posts = wp_count_posts(); $draft_posts = $count_posts->draft; ?>
Counting pages status types are done in the same way as posts and make use of the first parameter. Finding the number of posts for the post status is done the same way as for posts.
<?php $count_pages = wp_count_posts('page'); ?>
wp_count_posts() can be used to find the number for post statuses of any post type. This includes attachments or any post type added in the future, either by a plugin or part of the WordPress Core.
wp_count_posts() is located in