Home >Backend Development >PHP Tutorial >Use wp_count_posts function in WordPress to count the number of posts, wordpresswpposts_PHP tutorial

Use wp_count_posts function in WordPress to count the number of posts, wordpresswpposts_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:01:40920browse

Use the wp_count_posts function in WordPress to count the number of posts. WordPresswpposts

Isn’t it cool to do a site-wide statistics? There are fewer and fewer long-term blogs. Why not give yourself a statistic to see how much effort you have put into this blog. Not only for yourself but also for visitors. wp_count_posts is a function used to count the number of articles in WordPress. It can count all types of posts. of articles (posts) and pages (pages).

Description
wp_count_posts is a function used to count the number of posts in WordPress. It can count all types of posts and pages.

Use

//获取文章数量
$postcount = wp_count_posts();
//获取页面数量
$pagecount = wp_count_posts('page');

Return value
The usage is roughly the same as the above two forms,
The use of this function is mainly on the return value,
The above two forms will return an object,
As follows: The values ​​may be different, but the structure is the same

object(stdClass)#296 (8) {
//已发布
 ["publish"]=>string(1) "7"
//定时发布
 ["future"]=>int(0)
//草稿
 ["draft"]=>int(0)
//正在编辑的?(有待考证)
 ["pending"]=>int(0)
//隐私
 ["private"]=>int(0)
//垃圾箱
 ["trash"]=>int(0)
//自动草稿
 ["auto-draft"]=>int(0)
//这个不知道,也没研究
 ["inherit"]=>int(0)
}

Default usage

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

<&#63;php 
$count_posts = wp_count_posts(); 
&#63;>

Get count of published status articles

To get the status type of published articles, you should first call the wp_count_posts() function and then check the "Published" attribute.

<&#63;php 
$count_posts = wp_count_posts();  
$published_posts = $count_posts->publish; 
&#63;>

If you are using PHP5 and just want to get the status of an article, you can use the following more convenient method. This code will not run in PHP4, so if you want to maintain backend compatibility, you should choose the above code.

<&#63;php 
$published_posts = wp_count_posts()->publish; 
&#63;>

Draft Count

The method for calculating the number of drafts is the same as the method for obtaining the publication status count.

<&#63;php 
$count_posts = wp_count_posts();  
$draft_posts = $count_posts->draft; 
&#63;>

Page Count

The method of calculating the number of page types is the same as calculating the number of articles, and you also need to use the first parameter. Finding the number of pages in a certain state is the same as finding the logs in that state.

<&#63;php 
$count_pages = wp_count_posts('page'); 
&#63;>

Other uses

wp_count_posts() finds the number of any post type in a post state, including attachments and any post types that will be added. This can also be achieved through a plugin or part of the WordPress core team.

Parameters

type

(Character) The type of row in wp_posts, used to check which type corresponds to post_type. Default post.

perm

(character) This parameter can count the private article status into the article status, use 'readable' and require the user to log in. The default is empty string.

Articles you may be interested in:

  • Analysis of related functions used for title display in WordPress development
  • Analysis of related PHP functions used for debugging thumbnails in WordPress
  • Configuration to solve the problem of WordPress paths not automatically adding slashes in the Nginx server
  • Usage analysis of the PHP function used to obtain the search form in WordPress
  • Detailed explanation of calling comment templates and loop output in WordPress Comment PHP function
  • Detailed explanation of the use of classification function wp_list_categories in WordPress
  • WordPress restricts non-admin users to only comment once after an article
  • Detailed explanation of creation and addition in WordPress Filter related PHP functions
  • Detailed explanation of the usage of wp_title() function in WordPress development

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1088778.htmlTechArticleUse the wp_count_posts function in WordPress to count the number of articles. Wouldn’t it be cool to do a site-wide statistics with wordpresswpposts? There are fewer and fewer long-term blogs, why not give yourself a statistic...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn