: This article describes how to use the wp_count_posts function in WordPress to count the number of articles. if you are interested in the PHP Tutorial, refer to it. Is it cool to make a full-site statistics? There are fewer and fewer blogs for a long time. why don't you give yourself a statistical review of how much effort you have made on this blog, not only for yourself but also for tourists, wp_count_posts is a function used in WordPress to count the number of articles. it can count all types of articles (post) and pages (pages ).
Description
Wp_count_posts is a function used in WordPress to count the number of articles. it can count all types of articles (post) and pages (pages ).
Use
// Get the number of articles $ postcount = wp_count_posts (); // get the number of pages $ pagecount = wp_count_posts ('page ');
Return value
The above two forms are generally used,
This function is mainly used in the return value,
The above two forms will return an object,
Possible values are not the same, but the structure is the same.
Object (stdClass) #296 (8) {// published ["publish"] => string (1) "7" // timed release ["future"] => int (0) // draft ["draft"] => int (0) // is being edited? (To be verified) ["pending"] => int (0) // privacy ["private"] => int (0) // garbage bin ["trash"] => int (0) // automatic draft ["auto-draft"] => int (0) // unknown, I have not studied ["inherit"] => int (0 )}
Default usage
By default, the count of published articles is returned. This is an object. you can use var_dump () in the content to debug the output result.
<?php $count_posts = wp_count_posts(); ?>
Retrieve the count of published articles
To obtain the status type of published articles, call the wp_count_posts () function and check the "post" attribute.
<?php $count_posts = wp_count_posts(); $published_posts = $count_posts->publish; ?>
If you use PHP5 and only want to get the status of an article, you can use the following more convenient method. This code cannot be run in PHP4, so if you want to maintain background compatibility, you should select the above code.
<?php $published_posts = wp_count_posts()->publish; ?>
Draft count
The method for calculating the number of drafts is the same as that for obtaining the count of the publication status.
<?php $count_posts = wp_count_posts(); $draft_posts = $count_posts->draft; ?>
Page count
The method for calculating the number of page types is the same as the calculation article. The first parameter is also required. The number of pages in a status is the same as the log method in this status.
<?php $count_pages = wp_count_posts('page'); ?>
Other usage
Wp_count_posts () can be used to find the number of log types in the log status, including attachments and any log types to be added. It can also be achieved through plug-ins or some of the WordPress core teams.
Parameters
Type
(Character) type of the row in wp_posts, used to check which type corresponds to post_type. The default value is post.
Perm
(Character) this parameter can be used to calculate the private Article status into the article status, use 'readable' and ask the user to log on. The default value is empty string.
The preceding section describes how to use the wp_count_posts function in WordPress to count the number of articles, including related content. if you are interested in the PHP Tutorial.