Every now and then you would need to display the total post count in your WordPress application (website). There are several methods you can use for such a simple task, but can you tell the difference and choose the right method (function) which runs faster and consumes less resources? I’d like to share my findings with you in this post.
1. Get total post count
Since version 2.5.0, WordPress came with a built-in function called wp_count_posts
, which can be used to “count number of posts of a post type and if user has permissions to view.” So there are 2 parameters for this function: $type
and $perm
.
$type
can be available post types in your WordPress, like “post”, “page” or any custom post type. $perm
takes an empty string by default, you can change it to “readable
“, so if the user is logged in, it only counts posts are readable by the user.
wp_count_posts()
will return an object with the post statuses as the properties, you need to use syntax like this to retrieve the count of published posts:
$count = wp_count_posts();
$publish_post_count = $count->publish;
It will be a little bit tricky if you want to get the count of private posts. By default, if the user isn’t logged in, wp_count_posts()
won’t include “private” status into its properties.
In the function description, the developer told us there’s another method to count posts which “is to count the amount of items in get_posts()
, but that method has a lot of overhead with doing so.” Now we know wp_count_posts()
 runs with better performance but with it, we can only specify the post type of posts, we must find other solutions if we need to count posts in certain category or tag, or even count posts from a complex query.
2. Count posts in a category or tag
You might not think of it right away, but you must see it many times. Yes, I’m talking about the “Count” field in your Categories admin panel. Here’s a screenshot of mine:
WordPress stores the count number of posts of a category (term) in the database, in the count
column of $wpdb->_term_taxonomy
table exactly. So if we can access a term
object in WordPress, we can get the count number of posts in that term. For example:
$term = get_term( 1, 'category' );
$count = $term->count;
3. Count posts from a not-so-simple query
WordPress is very powerful and the power comes from tons of built-in classes and functions, among them I love WP_Query
most. It gets evolved almost every time WordPress gets upgraded. Now we can use meta_query
, tax_query
 and date_query
to accomplish complex post queries.
In such scenario, say if we’d like to know how many posts have the meta key / value pair “foo = bar”, no doubt we would use WP_Query
to query posts we need. Let’s see how can we query posts and get total post count at the same time:
As you can see, the found_posts
property does the trick. We can get total post count without query them all (by setting posts_per_page
t0 -1
).
If you’ve read some articles about optimizing WordPress queries, and developed a good habit to set “no_found_rows
” to true
when you don’t need pagination in your WP_Query, please note this is not the case. If we set “no_found_rows
” to true, the found_posts
property will be set to 0, and this method won’t be available for you.
4. What about counting items in get_posts()?
If you must use get_posts()
to query posts and count the results array with PHP function count
to get the total post count, I’ll say please must set these 2 arguments: "cache_results => false"
and "fields => ids"
.
They can help you to use minimum resources when getting posts with the greedy monster “posts_per_page = -1
“.
5. Get total post count with WP REST API
WP REST APIÂ returns the total post count in the response headers. Here’s a snippet from my AngularJS WordPress Theme:
In line 8, I pass the headers
 to the success callback function so I can get the response headers returned by WP REST API. In line 13 I use console.log
to display the value I want, which is “X-WP-Total
“. If we trace back to the source code of WP REST API, we can see that the “X-WP-Total
” is exactly the found_posts
property from WP_Query.
6. Install Query Monitor on every WordPress application you develop
It’s impossible that a professional WordPress developer can do her / his job right without Query Monitor. I used to disable the admin bar on the front end but now I enable it on every WordPress application I develop. Why? Because I need to check the performance overview from Query Monitor displayed at the admin bar.
After activating the Query Monitor you should be aware if there’s a db.php
in your wp-content
directory. If you can’t see the file or it’s not a symlinked file, Query Monitor can still work but you can’t see some extended information. To fix this, you can follow the instruction on the wiki page of this plugin.
I hope you enjoy these findings as much as I do. Please feel free to let me know your thoughts or share some tips with me. I’ll be back in April with new AngularJS WordPress theme tutorials. See you then!
Leave a Reply