Use the wp_count_posts function in WordPress to count the number of articles

Source: Internet
Author: User
This article mainly introduces how to use the wp_count_posts function in WordPress to count the number of articles. It can also calculate the number of drafts and pages, which is quite practical, if you need a friend, can you refer to the next full-site statistics? is it cool? 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.