Display Posts from Current Month & Year in a WordPress Template

Hi Y’all,
I was presented with this challenge while putting together a magazine-type site, where the home page was to display articles from the current issue–meaning the current month and year. Checking the forums, I found some solutions, and although the topics have been closed, I have found that they are incomplete. Some call the current month, i.e. “March”, but don’t not discern the current year, showing posts from March 2013, March 2012, etc. And some simply don’t work at all, sorry to say.

Here’s what I came up with, and have proven to work…

 $current_year = date('Y', current_time('timestamp'));
 $current_month = date('m', current_time('timestamp'));
 if(have_posts()) : while(have_posts()) : the_post();
<?php endwhile; endif; wp_reset_query(); ?>

Of course, there is room to pass way more arguments within query_posts(), but that’s the easy part.


One Response to “Display Posts from Current Month & Year in a WordPress Template”

  • Ted says: Nov 4, 2014 at 3:13 am

    Hi Doug Would you ever have a situation where, on a given date, there aren't yet any posts for the current month? If so, how did you address the situation when the month rolls over and, at that time at least, there might be no current month posts to display? In my case, there are only a few posts each month. I'm thinking that if, on a given date, there are no posts for the current month, then I'll just display the posts for the previous month. But I'm keen to hear other strategies. Regards Ted

Leave a Comment