Home Backend Development PHP Tutorial Use wp_count_posts function to count the number of posts in WordPress_php tips

Use wp_count_posts function to count the number of posts in WordPress_php tips

May 16, 2016 pm 08:00 PM
php wordpress article count

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');
Copy after login

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)
}

Copy after login

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;>
Copy after login

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;>
Copy after login

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;>
Copy after login

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;>
Copy after login

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;>
Copy after login

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.

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles