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
[numberposts author="marty"]
I can return the result of my own published posts. The result: 575. To display the number of scheduled posts for a user, include status="future"
to the shortcode with a result of 0. The show the number of my draft posts, the shortcode of [numberposts author="marty" status="draft"]
is used with a result of 38.
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
[numberposts]
) returns 575. Date Range
[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
[numberposts status="future"]
. The result: 0.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
status
of the post. Normally publish, future, draft, or page. Custom post types may be used. See a full list here .author
category
category
.tags
tags
type. Use one or more.days
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
after
a specific date, use after="2nd June 2017"
. Accepts a strtotime()
-compatible string.after
before
a specific date, use before="15th June 2017"
. Accepts a strtotime()
-compatible string.cache
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: Display the Total Number of WP Posts (WordPress Plugin)
Description: Display the Total Number of Scheduled, Draft, or Published Posts (or by Author) with WordPress Shortcode.
Download • Version 0.2, 2.0K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (1.4K)