Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 19:51:45 09月25日 Update

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!

Tags: Article quantity state type function method page statistics draft content parameter use in code character object form log is in article convenient Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Technology Microsoft Redmi Docker