Back to Top

Get the Number of Posts for Each Post Type

Previous Post:

Get the Number of Posts for Each Post Type

The magic function we are going to use is wp_count_posts(), which is available since WordPress version 2.5.0:

This function 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.

Function Reference/wp_count_posts

Now if you have a custom post type named ‘movie’ and you want to display the total number of movies you own, you can do something like this:

// get an object containing all post statuses of the post type 'movie'
$count = wp_count_posts('movie');
// show the total number of movies you own
printf(__('Total number of movies: %s'), $count->publish);
// or save the string for later use
$total_string = sprintf(__('Total number of movies: %s'), $count->publish);

But you might also want to show the number of movies you are going to buy soon, in such case, you can schedule some movies to be published in the future and use this instead:

// show the total number of movies you are going to buy
printf(__("I'm going to buy %s more DVDs!"), $count->future);

If you care about the plural and singular form of the string, you can use _n() function1, like so:

printf(_n(__("I'm going to buy %s more DVD!"), __("I'm going to buy %s more DVDs!"), $count->future), $count->future);

Below is the full list of supported post statuses that you can use:

future — scheduled
draft — unfinished
pending — being reviewed
private — viewable by users with 'read_private_{post_type}s' capability
inherit - post that have a parent

One limitation with this function is that you can not count more than one post types at one time and it will not be efficient if you call this function multiple times, especially if you have a lot of posts in your database (even with WordPress caching on). Nevertheless, just do it if you have to:

$count_post_typea = wp_count_posts('typea');
$count_post_typeb = wp_count_posts('typeb');
$total = (int) $count_post_typea->publish + (int) $count_post_typeb->publish;
printf(__('Total number of posts: %s'), $total);

I will address this issue in a follow-up to this tip, so stay tuned ;)!


  1. ... Developers []

Take Social Sharing to
the Next Level with Monarch!

Take Social Sharing to the Next Level with Monarch!
Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS

2 Opinions for Get the Number of Posts for Each Post Type

  1. User's Gravatar
    Paul September 1, 2012 at 10:38 am – Permalink

    How would I display the number of custom posts within a category of custom posts?
    i.e. the number of events (custom post type) in Florida (the events category)

    Want to display this number externally on the site.

    Thanks for any help!!

    • User's Gravatar
      OddOneOut September 3, 2012 at 10:21 pm – Permalink

      For that I believe you will have to use custom query 🙂

Speak Up Your Mind!

An asterisk (*) indicates a required field and must be filled.

  • Web page and e-mail addresses turn into links automatically.
  • Wrap codes in: <code lang=""></code> or <pre lang="" extra="">
  • Lines and paragraphs break automatically.

Next Post: