I’ve published over 400 Posts on my WordPress Website over the years. Most Posts relate to WordPress or Search Engine Optimization (SEO) and range from thoughts on SEO right through to more technical WordPress development tips. There are various Plugins and widgets that allow for easily showing the number of Posts in a Category, Tag or per Author but there doesn’t seem to be a simple means of calculating and showing a count of the total number of Published Posts. Fortunately this is easily achieved using the steps below.
How to calculate the number of Posts Published
The MySQL database is where a count of the number of published Posts needs to take place but we can’t run a SQL query directly from the Website therefore we use some basic PHP like this using the wp_count_posts WordPress function:
$published_posts = wp_count_posts();
So the script above will run a query on the database and then will return and display the value (the ‘echo’ bit tells the script to display the result). But how do we display the value on a Page, Post or Widget.
How to run PHP in a Widget
In this paragraph I’ll cover how to output/display the PHP in a widget. There really is no point in reinventing the wheel when the functionality required already exists and works well. Go ahead and install the PHP Code Widget which is a free WordPress Plugin and does as it’s title suggests.
Once you’ve installed and activated the PHP Code Widget you’ll need to add the new widget into the widget area where you’d like your Post count to display.
Show and Style your total number of Posts
So now you’ll have a number output in the widget that shows the number of Posts you’ve published on your Blog. A number alone doesn’t mean much to anyone so you’ll probably want to put some text before or after the number as well as add style how the text and number in the widget look. This is easily done by using some HTML and/or CSS. For example the image below shows the HTML and CSS I’ve used to create the desired output in my widget:
So there it is, a really simple way to show the number of Posts you’ve published on your WordPress Website.