Count the number of posts in the custom post type in WordPress
WP_Query is one of the WordPress classes that you can use to count the number of posts in a custom post type.
A custom post type is a group of post involving contents that share some features eg. category, post tag names, or published posts
The WP_Query contains the variables that retrieve the posts by category name, a post type, and the property to get the number of posts in a specified category.
These variables are $found_posts, $post_type and category_name.
However, This multi-tasks class requires those variable values to be defined in an array.
Before going into more details, watch out for the shortcode that will display the number of posts for the specific category.
$postTypes = new WP_Query($post-args);
Line-1 creates an array object. The array contains two elements, the elements are WP_Query properties.
The properties for retrieving post types in the specific category are category_name and post_type.
The property category_name is the category slug and not the actual category name.
So, a line-2 defines a slug of a category to be ‘things-to-do’
Also, line-3 specifies the types of posts to be returned. Here, I want to get all the posts on a type of street-food.
The constructor of WP_Query passes the array which contains the data of specified elements.
Hence Line-5 creates $postsTypes object with a constructor that receives an array of member variables category_name and post_type.
In order to get the total number of posts, the $postTypes object calls a variable found_posts in the Line-6.
Line-7 displays the post types count in the specified WordPress taxonomy.
wp_count_posts for number of posts.
wp_count_posts return the object which retrieves the number of posts of a given WordPress status.
This post’s status counter function gives the number of certain post status and not post type.
You should echo the function to display the number of posts returned.
<?php echo wp_count_posts();?>
WordPress status includes posts, published posts, categories, drafted posts, pages, and so on.
wp_count_posts receives two optional parameters, which are $type and $perm both are string variables.
Let us take a look at the $type variable.
$type lets you specify the type of page that you want to know its quantities.
For example, I want to know how many pages my blog has.
The status is “page”, hence I will pass a string “page” to wp_count_posts to get the number of all pages.
<?php echo wp_count_posts("page");?>
So, if I want to display the number of the published pages I will call the publish member variable.
<?php echo wp_count_posts("pages")->publish;?>
This is not the only WordPress function to count the number of posts.
Another function is get_posts.
But it is bad practice to implement get_posts to get the total posts the blog has.
The next section tells you the main reason.
Using get_posts to count the number of posts.
get_posts passes the array parameters to return the array of posts.
The array should the elements that define the post type and status of each post.
- echo count($number);
The above snippet will display the number of posts in the custom WordPress taxonomy.
But get_posts may create a memory problem if the blog has thousands of posts.
This is because the function returns the array of current posts meaning that it first adds each post to the array.
WP_Query vs wp_count_posts vs get_posts
In my opinion, WP_Query is a more flexible class that can be used to call the functions and properties which return various output.
In counting posts of a custom taxonomy, it is better to use WP_Query properties instead of wp_count_posts and get_posts.
wp_count_posts just count the posts of certain status. you can’t use category slug.