Home>

Isn't it cool to do a total station statistics?There are fewer long-term blogs,Why not give yourself a statistic,See how much you have worked on this blog,Not only to myself but also to tourists,wp_count_posts is a function used to count the number of posts in wordpress.Can count all types of posts and pages.

descriptionwp_count_posts is a function used to count the number of posts in wordpress.Can count all types of posts and pages.

use

// Get the number of articles
$postcount=wp_count_posts ();
// Get the number of pages
$pagecount=wp_count_posts ("page");

return valueIt is roughly the same as the above two forms.

The use of this function is mainly on the return value,

Both forms will return an object,

As follows:the values ​​may be different,But the structure is the same

object (stdclass) #296 (8) {
//Published
 ["publish"] =>string (1) "7"
// Timed release
 ["future"] =>int (0)
//draft
 ["draft"] =>int (0)
// Editing?(To be verified)
 ["pending"] =>int (0)
//privacy
 ["private"] =>int (0)
// trash can
 ["trash"] =>int (0)
// Automatic draft
 ["auto-draft"] =>int (0)
// I don't know,Didn't research
 ["inherit"] =>int (0)
}

Default usage

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

<?php
$count_posts=wp_count_posts ();
?>

Get Count of Post Status Articles

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

<?php
$count_posts=wp_count_posts ();
$published_posts=$count_posts->publish;
?>

If you use php5 and want to get only one article status,You can use the following more convenient methods.This code does not work in php4,So if i want to maintain background compatibility,The above code should be selected.

<?php
$published_posts=wp_count_posts ()->publish;
?>

Draft count

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

<?php
$count_posts=wp_count_posts ();
$draft_posts=$count_posts->draft;
?>

Page count

Counting the number of page types is the same as counting articles,You also need to use the first parameter.Finding the number of pages in a state is the same as finding logs in that state.

<?php
$count_pages=wp_count_posts ("page");
?>

Other uses

wp_count_posts () finds the number of any post type in the log state,Including attachments and any log types that will be added,This effect can also be achieved by some core teams of plugins or wordpress.

parameter

type

(Character) the type of line in wp_posts,Used to check which type corresponds to post_type. The default post.

perm

php
  • Previous Json data analysis to simulate the US Mission interface display
  • Next Android development method using GridView to display pictures
  • Trends