count-posts-shortcode

Display the Total Number of Scheduled, Draft, or Published Posts (or by Author) with WordPress Shortcode

It was nearly 5 years ago I published a one-line shortcode that would publish the total number of published WordPress posts with shortcode (using wp_count_posts() ). Since then I’ve obviously had the need for more features. The code on this page will accomplish the following:

  • Display the total number of queried articles by all authors.
  • Display the total number of articles by a single author.
  • Query posts by live posts, drafts, or scheduled posts.
  • Display the number of posts by an author (or all authors) in the last number of days.

The Result

User Posts

Using the shortcode of [numberposts author="marty"] I can return the result of 350. If I were to include the number of scheduled posts and drafts (in addition to those that are published) I’d include future="1" drafts="1" to the shortcode will a result of 395. If I were to return the number of Ken’s posts I’d use [numberposts author="ken"] with a result of 10.

Total Posts

To return the number of published articles you should omit the author parameter ([numberposts]); in our case returning 361.

Date Range

We can display the number of posts published (or other posts with the use of available attributes) prior to today with the shortcode of [numberposts author="marty" days="28"] with a result of 0.

Scheduled Posts Only

To display only the number of scheduled posts for all users we should use [numberposts current="0" future="1"] (future="1" will only query unpublished posts). The result: 29.

The Shortcode Function

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

Shortcode Attributes

author

The author is either the login name or the user ID.

current

If current="0" we will only count unpublished posts. Defaults to true.

drafts

If drafts="1" we will include the count of posts in drafts. Defaults to false (excluded).

future

If future="1" we will include the count of scheduled posts. Defaults to false (excluded).

days

days is the number of days prior to today to include in the search. For example, to count only the number of posts published in the last 7 days, use days="7". For one month, use days="28", and so on.

n

To return the count in a number format (formatted in groups of thousands), use n="1". To disable, use n="0". True by default.

cache

The cache is the amount of time to store the results in your database. It caches for two days by default.

Considerations

It’s expected that you will alter the shortcode function to reflect your preferred default output so only minimal attributes will be required to return your desired result.

You could render the results as a readable list rather than count them. While we’ve done this sort of thing in the past, we may update this code to provide that feature.

Make sure you follow us on Facebook for updates.

Download the Code

Download the shortcode function here .

Shortt URL for this post:

Leave a Reply

Your email address will not be published. Required fields are marked *