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.
Following are examples of how the shortcode might be used.
[numberposts author="marty"]I can return the result of my own published posts. The result: 348. 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 62.
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).
[numberposts]) returns 348.
[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 (
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.
statusof the post. Normally publish, future, draft, or page. Custom post types may be used. See a full list here .
tagstype. Use one or more.
daysattribute 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.
aftera specific date, use
after="2nd June 2017". Accepts a
beforea specific date, use
before="15th June 2017". Accepts a
cacheis the amount of time to store the results in your database. It caches for 6 hours by default.
- See also: Render future posts in a readable list.
wp_queryfunctions 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.