I'm creating an events site for a theatre in wordpress - at the moment I have set up a custom post type for Events and am using a custom field 'event_date' (which formats to YYYY-MM-DD) to order the events on their listing page and also move them to the archive if past todays date.

So far all working nicely.

Next up I need to create an month by month filter for the months in which shows appear however am a bit stumped. I guess I need to: - check the months which contain shows (not past) and output a list link these months to a template which filters to just show the relevant month..

Any pointers?

Thanks in advance

2 Answers 2


Ok short of a better solution here is what I've come up with:

   $today = date('Y-m-d', strtotime('-6 hours')); 
      'post_type' => 'events', 'posts_per_page' => 99, 'meta_key' => 'event_date', 'orderby' => 'meta_value', 'order' => 'ASC', 
      'meta_query' => array(array(
         'key' => 'event_date',
         'meta-value' => $value, 
         'value' => $today, 
         'compare' => '>=', 
         'type' => 'CHAR'
   if (have_posts()) :
   while (have_posts()) : the_post();

The above gets all events in future of todays date

    // http://stackoverflow.com/questions/3586384/wordpress-events-grouped-by-month
    // for each month output the month once 
    $eventdate = get_post_meta($post->ID, "event_date", true);
    if(!isset($currentMonth) || $currentMonth != date("m", strtotime($eventdate))){
    $currentMonth = date("m", strtotime($eventdate));

    //make the month to pass into the url
    $month = date("Y", strtotime($eventdate)).'-'.date("m", strtotime($eventdate));
    $monthname = date("F", strtotime($eventdate));

Kick out the month name once if posts exist in that date and manipulate for output.

    <a href="<?php echo home_url( '/' ); ?>whats-on-page/shows-by-month/?startdate=<?php echo($month); ?>-01&enddate=<?php echo($month); ?>-31&monthname=<?php echo ($monthname); ?>">
        <?php echo ($monthname); ?>
<?php } ?>

Output the html so we have a li and link - link goes to a page using a custom page template which pulls the parameters passed in the URL into a meta query like below:

   $start = $_GET["startdate"];
   $end = $_GET["enddate"];
   $monthname = $_GET["monthname"];
   // gets info from URL

      'post_type' => 'events', //query “events”
      'posts_per_page' => 10,
      'paged' => $paged,
      'meta_key' => 'event_date',
      'orderby' => 'meta_value',
      'order' => 'ASC', //sort in ascending order
        'meta_query' => array(
                'key' => 'event_date',
                'value' => array($start, $end),
                'compare' => 'BETWEEN',
                'type' => 'DATE'
        ) // meta query between dates
   if (have_posts()) :
   while (have_posts()) : the_post();

So now I have a dynamic listing in the sidebar to link to a custom page template which filters by month, effectively recreating the post archive effect using a custom date field.

Sure I could probably create some URL rewrites to make this work with pretty URLs too but thats for another day.


Create an archive index for your custom post type (see http://codex.wordpress.org/Creating_an_Archive_Index) first.

I grabbed the next part from another website (http://wpgarage.com/code-snippets/how-to-create-an-archive-based-on-custom-fields-not-publish-date-in-wordpress/)

I found this wonderful date field plugin (http://matth.eu/wordpress-date-field-plugin) which lets  you sort posts by the date entered in the custom fields.  Here’s what I did:

  1. Upload the plugin and activate it

  2. Opened the archive page template you created and added a query. The query checks for posts that have a date filled in and the orders it from oldest to newest.

<?php query_posts('meta_key=date_value&orderby=meta_value&order=DESC'); ?>

  1. I replaced a typical date function within the loop <?php the_time(‘j M Y’); ?> with

<?php echo date(“l jS \of F Y”, get_post_meta($post->ID, ‘date_value’, true));?>

You'll now have a list of posts by date, in your archive.. From there it shouldn't be to hard to take the next step and filter by dates etc.

  • Thanks Simon, this is where I'm up to already I have two archives but generating the upcoming events by month in the sidebar is the tricky bit.
    – tbwcf
    Commented Sep 6, 2012 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.