Do you want to display the post, page, and comment counts via a template? Here’s a selection of snippets that help you do that. This snippet lets you show how many posts, pages and custom posts are published and are saved as draft. You can also show the number of comments under these lists: Pending, approved, spam, trash and total.
All you have to do is add these snippets to your template file.
<?php // count posts $n_post = wp_count_posts(); $n_post_publish = $n_post->publish; $n_post_drafts = $n_post->draft; // count pages $n_page = wp_count_posts('page'); $n_page_publish = $n_page->publish; $n_page_drafts = $n_page->draft; // count custom post types $n_cpt = wp_count_posts('CUSTOM_POST_TYPE_NAME'); $n_cpt_publish = $n_cpt->publish; $n_cpt_drafts = $n_cpt->draft; // count comments $n_comments = wp_count_comments(); $n_comments_moderated = $n_comments->moderated; $n_comments_approved = $n_comments->approved; $n_comments_spam = $n_comments->spam; $n_comments_trash = $n_comments->trash; $n_comments_total = $n_comments->total_comments; ?>
<ul> <!-- posts --> <li>Post publish: <? echo $n_post_publish; ?></li> <li>Post drafts: <? echo $n_post_drafts; ?></li> <!-- pages --> <li>Page publish: <? echo $n_page_publish; ?></li> <li>Page drafts: <? echo $n_page_drafts; ?></li> <!-- custom post types --> <li>Custom post publish: <? echo $n_cpt_publish; ?></li> <li>Custom post drafts: <? echo $n_cpt_drafts; ?></li> <!-- comments --> <li>Coments moderated: <? echo $n_comments_moderated; ?></li> <li>Coments approved: <? echo $n_comments_approved; ?></li> <li>Coments spam: <? echo $n_comments_spam; ?></li> <li>Coments trash: <? echo $n_comments_trash; ?></li> <li>Coments total: <? echo $n_comments_total; ?></li> </ul>
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: Best WordPress Page Builders (Compared).