and like us on FB

A number of 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 or a single authors.
  • Filter by post status, tag, category, or post type.
  • Display the number of posts by an author (or a combination of authors) in the last number of days.
  • Display the number of (filtered) posts in a date range.

All the results are cached to avoid the overhead with repeated queries.

The Result

Following are examples of how the shortcode might be used.

User Posts

Using the shortcode of [numberposts author="marty"] I can return the result of my own published posts. The result: 151. To display the number of scheduled posts for a user, include status="future" to the shortcode with a result of 1. The show the number of my draft posts, the shortcode of [numberposts author="marty" status="draft"] is used with a result of 51.

To include the total posts by multiple authors, include them in a comma delimited list as follows: [numberposts status="publish" author="marty,3"] (note I’ve included login names and user IDs).

Total Posts

To return the total number of published articles you should omit the author parameter. Shortcode of [numberposts]) returns 151.

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.

You may return the number of posts between two dates with the following: [numberposts author="marty" from="1st June 2017" before="15th June 2017"]. In this case, the result is 30. You may optionally use one or the other (before or after). If you specify either with the days attribute, the date as just described will be ignored.

Scheduled (Future) Posts

To display only the number of scheduled posts for all users we should use [numberposts status="future"]. The result: 1.

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

status

The status of the post. Normally publish, future, draft, or page. Custom post types may be used. See a full list here .

author

The author is either the login name or the user ID. Multiple may be used.

category

Returns a count of posts by category.

tags

Returns a count of posts by tags type. Use one or more.

days

The days attribute is the number of days prior to today. 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.

after

To count the number of posts after a specific date, use after="2nd June 2017". Accepts a strtotime()-compatible string.

after

To count the number of posts before a specific date, use before="15th June 2017". Accepts a strtotime()-compatible string.

cache

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

Considerations

  • See also: Render future posts in a readable list.
  • The wp_query functions return enormous amounts of information. We’ve written the function to filter the most common parameters. If you require additional filtration, let us know and we’ll update the code.

Download

Title: Count WordPress Posts
Description: Display the Total Number of Scheduled, Draft, or Published Posts (or by Author) with WordPress Shortcode.
Download: Shortcode (V0.2) | WP Plugin (V0.2) | | Plugin Page

Shortt URL for this post: http://shor.tt/2GLH