Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use wp_count_posts function to count the number of articles in WordPress

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use the wp_count_posts function to count the number of articles in WordPress. The article is very detailed and has a certain reference value. Interested friends must read it!

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 (page).

Use

/ / get the number of articles $postcount = wp_count_posts (); / / get the number of pages $pagecount = wp_count_posts ('page')

Return value

In use, it is roughly in the above two forms.

The function is mainly used on the return value.

Both of the above forms return an object

As follows: the value may be different, but the structure is the same.

Object (stdClass) # 296 (8) {/ / released ["publish"] = > string (1) "7" / / scheduled release ["future"] = > int (0) / / draft ["draft"] = > int (0) / / what is being edited? (to be verified) ["pending"] = > int (0) / / Privacy ["private"] = > int (0) / / dustbin ["trash"] = > int (0) / / automatic draft ["auto-draft"] = > int (0) / / I don't know about this, and I haven't studied ["inherit"] = > int (0)}.

Default usage

The default usage returns the count of published articles. This is an object, and you can debug the output with var_dump () in the content.

Get the count of published status articles

To get the status type of a published article, you should first call the wp_count_posts () function and then check the publish property.

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 choose the above code.

Draft count

The method of calculating the number of drafts is consistent with the method of obtaining publication status counting.

Page count

The same way to calculate the number of page types as for articles, you need to take advantage of the first parameter. Finding the number of pages in a state is the same as finding logs in that state.

Other uses

Wp_count_posts () can find the number of any log types in the log state, including attachments and any log types that will be added, and this can also be achieved through plug-ins or some core teams in WordPress.

Parameters.

Type

(character) the type of line in wp_posts that is used to verify which type corresponds to post_type. Default post.

Perm

(character) this parameter counts the private article status into the article status, uses' readable', and requires the user to log in. The default is empty string.

The above is all the contents of the article "how to use the wp_count_posts function to count the number of articles in WordPress". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report