Very convenient and fast function that allows you to get the number of posts of a certain type of records, as well as any status, even unpublished and deleted.
In principle, when working with WP_Query(), you can also get the number of posts according to your request, but do you need to do this in each case, or maybe you want to get the number of posts that have not only published status.
wp_count_posts ( $type, $perm );
(string) (optional) specify the name of the type of records that we want to count. Default: ‘post’.
(string) (optional) allows you to make so that the user’s personal (private) posts are added to the counting of the number of records. To do this, specify ‘readable’.
Returns an object with the number of posts for each status. Here is an example of what happens:
stdClass Object ( [publish] => 23 [future] => 0 [draft] => 0 [pending] => 0 [private] => 0 [trash] => 0 [auto-draft] => 0 [inherit] => 0 [request-pending] => 0 [request-confirmed] => 0 [request-failed] => 0 [request-completed] => 0 )
The most important thing to keep in mind is that the function returns an object, and not just the number of published posts. Now I will give a simple example of how to display the number of published posts for your custom post type:
$count_posts = wp_count_posts('services'); //specify the type of record you created - services $published_posts = $count_posts->publish; //number of posts published only echo $published_posts; //we derive the received quantity