We've moved! Check us out at Graphic Junkie. A new design inspiration website.

Post Pic

Use PHP To Count Your New Wordpress Posts

2 Comments

In the process of redesigning this blog into something a little more modern I was trying to find out how to count the number of new posts that have been posted within a certain number of days. After searching for days on end I came across the answer on the Wordpress forums.

More in Tutorials | December 8th, 2009

You may have noticed that on this blog all new posts are marked with a “new” badged, which I detailed how to do here. It’s badge that lasts for three days from the time it was posted. The issue I was having is actually counting ALL posts that were posted within my desired set amount of time, say, two days. Let the journey begin.

The first thing I ran into was a bit of PHP to count all posts in a certain year. This is what I found:

<?php $count=0; $my_query = new WP_Query('year=2008');
while ($my_query->have_posts()) :
$my_query->the_post();
$count++; endwhile;
echo '2008 post count is ' . $count; ?>

It’s not a bad function, kind clever if you think about it, but not at all what I needed. So my search went on. After consulting all kinds of forums and doing all kinds of searches, I was finally helped by one of the moderators on the Wordpress.org forums. This is what MichaelH came up with:

<?php
  function filter_where($where = '') {
    //posts in the last 90 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-90 days')) . "'";
    return $where;
  }
add_filter('posts_where', 'filter_where');
    $args=array(
      'post_type' => 'post',
      'post_status' => 'publish',
      'showposts' => -1,
      'caller_get_posts'=> 1
      );
$my_query=new WP_Query($args);
remove_filter('posts_where', 'filter_where');

  if( $my_query->have_posts() ) {
    echo '<h2>Number of posts in last 90 days is '.count($my_query->posts) . '</h2>';
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <p><small><?php the_time('m.d.y') ?></small> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
     <?php
    endwhile;
  } //if ($my_query)
wp_reset_query(); //just in case
?>

This was basically what needed, but I had to go ahead and modify it a bit. What this gives you is a total count of all new posts in the last 90 days and that’s it. I wanted to be more specific, counting all the new posts in the last two days from a certain category… and I wanted to do it twice on the same page. This is what the end result would look like to run the function once and get one total.

<?php   function filter_blog($where = '') {
//posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-2 days')) . "'";
return $where;   }
add_filter('posts_where', 'filter_blog');
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 15,
'showposts' => -1,
'caller_get_posts'=> 1
);
$my_blog=new WP_Query($args);
remove_filter('posts_where', 'filter_blog');
if( $my_blog->have_posts() ) {
echo count($my_blog->posts) . ' new';
while ($my_blog->have_posts()) : $my_blog->the_post(); ?>
<?php     endwhile;   }
//if ($my_blog) wp_reset_query(); //just in case ?>

Some of the modifications I made include placing the 'cat'=>15, which tells the function to not only look for published posts, but also for posts under that specific category. I've also removed the loop from the end of the function because I don't actually want to print any of the posts, I just want to print the count. If you want to run this function twice on the same page to count different new posts in different categories, you'll have to change the name of the function. Right now the name is filter_blog, so just change it to whatever you want everywhere in the function. You'll also have to change the name of the variable $my_blog. The value for that variable can only be set once.

    Related Posts
  1. Adding ‘New’ Badges To Your Wordpress Posts
  2. Automatically Add Images To Wordpress Posts
  3. Obama’s Transition team open for questions!
  4. Five Must-Have Wordpress Plugins
  5. A New Facebook That No One Likes, Except For Me

Tags: ,

2 Responses to “Use PHP To Count Your New Wordpress Posts”

  1. O'Ryan says:

    Just thought you should know that your text-shadow css effect on your copy makes your code virtually unreadable in Firefox (maybe others too i didn’t check). perhaps disable it for the code sections or make the color way lighter??