


Use wp_count_posts function to count the number of posts in WordPress_php tips
Wouldn’t it be 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.
<?php $count_posts = wp_count_posts(); ?>
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.
<?php $count_posts = wp_count_posts(); $published_posts = $count_posts->publish; ?>
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.
<?php $published_posts = wp_count_posts()->publish; ?>
Draft Count
The method for calculating the number of drafts is the same as the method for obtaining the publication status count.
<?php $count_posts = wp_count_posts(); $draft_posts = $count_posts->draft; ?>
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.
<?php $count_pages = wp_count_posts('page'); ?>
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
