Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

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.

share|improve this answer
    
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 Sep 6 '12 at 13:19
add comment

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

<?php
   $today = date('Y-m-d', strtotime('-6 hours')); 
   query_posts(array(
      '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

<?php
    // 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.

<li>
    <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); ?>
    </a>
</li>
<?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

   query_posts(array(
      '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(
            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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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