How to display the number of your published WordPress posts

Needing to provide information for a page for marketing companies, I created a shortcode that would output the number of posts that have been published. And it’s just 2 lines long!

Copy the code below into your theme’s functions.php file and then place the shortcode [post_count] wherever you wish the count of posts to appear.

<?php
function post_count_sc( $paras = '', $content = '' ) {
return wp_count_posts() -&gt; publish;
}
add_shortcode( 'post_count', 'post_count_sc' );
?>
view raw functions.php hosted with ❤ by GitHub

If you want to change the shortcode name, simply change it on the final line.

Short, sweet and does the job!

6 responses

  1. Joe Siegler avatar
    Joe Siegler

    Is there a way to do the same thing for total number of words?

    I used to have a plugin that did that, but when I went from Movable Type to WordPress, the same kind of plugin was a bloody system resource hog, had to remove it.

    1. That would be easy to do, but is it the kind of thing you’d want to display within a post via a shortcode?

      David.

      1. Joe Siegler avatar
        Joe Siegler

        In a post, no – but I’d like to use it in the sidebar.

        1. Sidebar? Are you wanting to display the word count for each post? Wouldn’t that be better situated with the actual post (either at the top or bottom of it)?

          David.

          1. Joe Siegler avatar
            Joe Siegler

            Sidebar on the main part of the site, not on the individual posts. I used to have that as a cgi plugin in Movable Type where it would post the total number of words over the entire blog, but when I went to WordPress, the same plugin was unsable, it took my server down.

          2. Hmmm. Unfortunately, that would be a big project as you’d have to track the word count of each post and then tot it up in total as required. I can imagine why the one you had had performance issues – unless it’s down right this is bound to happen.

            David.

Talk to me!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from David Artiss

Subscribe now to keep reading and get access to the full archive.

Continue reading