Counting Posts

Daniel at The Web Design Journal emailed me to ask about the post count block that appears in my sidebar. For example:

There are 2 posts in the last 30 days, and 323 total posts.

There are a number of ways to get the total post count. The last 30 days post count is a feature I wanted to add when I switched to WordPress and designed this theme. After searching for an existing solution, I ended up rolling my own. For reasons I don’t recall, I also wrote my own function for getting the total post count. I had meant to post it here, but had forgotten about it.

Instead of designing this as a WordPress plugin, I added it directly to template. In my sidebar template, I added the following:

<p>There are <?php echo get_post_count_inlast(30) ?> posts in the last 30 days, 
    and <?php echo get_post_count() ?> total posts.  
    <a class="sidelink" href="<?php echo get_bloginfo('wpurl')?>/archive/">Archives &raquo;</a></p>

The special bits here are the calls to get_post_count_inlast() and get_post_count(), which are simple PHP functions I wrote to query the WordPress MySQL database. I added these functions to functions.php in the theme directory for my theme. I’m not sure if all WordPress theme include this file, but the default theme, on which I based my theme, already provided this file. In this file, I added the code for the two new functions:

function get_post_count() {
    global $wpdb;
    return $wpdb->get_var('select count(*) from wp_posts where post_status = "publish"');

function get_post_count_inlast($days=30) {
    global $wpdb;
    return $wpdb->get_var("select count(*) from wp_posts where post_status = 'publish' and DATEDIFF(CURRENT_DATE, POST_DATE) <= $days");

I’m no PHP expert, I’ve picked up just enough to make the tweaks I wanted to my templates, but I believe you could even include the function definitions directly in the sidebar.php file, as long as they’re inside a <?php ... ?> tag.

Hope someone finds this useful. If you decide to add this to your site, please drop me a comment, and be sure to fill in your site URL.

(The source code in this post is released under the GNU GPL (just like WordPress), and comes with no warranty of any kind, express or implied.)

Both comments and pings are currently closed.

7 Responses to “Counting Posts”

  1. Danie (from The WDJ) Says:

    I have added this on bonAveo’s archive page.

  2. douglas.nerad Says:

    Couldn’t you have stolen some of this code (or all of it) simply by hacking into the admin pages? There on the dashboard is a summary similar to what you’ve given, including the number of comments and categories.

    I’m just saying…

  3. Jason Says:

    Sure Doug. In fact, I can’t remember why I wrote my own get_post_count; seems like WP already provides that as a function for templates. The tricky part (from a WP perspective) was counting posts in the last 30 days; I couldn’t find an existing function or plugin for it. Fortunately, it’s easy in SQL to limit results by date.

  4. Aleksandersen Says:

    You could not make one for counting comments as well?

  5. The K-log Says:

    […] the only downside is that it requires you to count all the posts, and writing another loop for that seemed a little excessive. i did a little more searching and found this, which offers a php function that will get the total number of posts for you. apparently people want to do that somewhat often, but wordpress doesn’t support it directly. yet. but i still love them. […]

  6. bensam Says:

    Douglas: It’s not “steal code”, it’s “borrow code”. :-)

  7. Jason Says:

    Bensam: I’m sure Doug was only quoting Picasso: “Bad artists copy, great artists steal.” :)