Filter events by year and month

This topic contains 2 replies, has 2 voices, and was last updated by  Stephen Harris 11 years, 3 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #2927

    Hi there,

    I am new to this plugin but it looks great.

    I just have a question about filtering the posts by year and month. Here is what I need:

    View All
    2013
    January
    February
    March etc…

    The months are only shown if there are events starting in that month. What I want is for the user to click on the month e.g. February and all events to show for that month only. Just like you can do to filter posts by category in the normal wordpress posts.

    Any help would be great.

    zebedeee
    #2928

    This is entirely possible, but I think would require a custom template.

    You can use eo_get_events() (see docs – or WP_Query with the appropriate attributes set) to retrieve events. Specifically you can use event_start_after /event_start_before to get events starting within two dates. E.g.:

     //Events for January 2013:
      $events = eo_get_events(array(
          'event_start_after'=>'2013-01-01',
          'event_start_before'=>'2013-01-31',
       ));
    

    If there are any you can display a link to your page (appending a query variable e.g. ?event-month=01. That page template can perform a query for the received month (e.g. $month = (int) $_GET['event-month'] and then using that in something like the above, e.g.:

      function zebedeee_get_events_for_month( $m, $y ){
    
         $first_day_of_month = new DateTime( $y.'-'.$m.'-01' );
         $last_day_of_month = new DateTime(  $first_day_of_month->format('Y-m-t') );
    
         //Events for this month
         return eo_get_events(array(
                       'event_start_after'=>$first_day_of_month,
                       'event_start_before'=>$last_day_of_month,
                  ));
       }
    
       /* Usage 
          $events = zebedeee_get_events_for_month(2013,01);
       */
    
    Stephen Harris
    #2929

    Incidentally,

    The following

    function zebedeee_register_query_vars( $qvars ){
        //Add these query variables
        $qvars[] = 'event_start_after';
        $qvars[] = 'event_start_before';
        return $qvars;
    }
    add_filter('query_vars', 'zebedeee_register_query_vars' ); 
    

    Will mean you can use ?event_start_after and ?event_start_before in the url (i.e. of the events archive – typically www.yoursite.com/events/event):

    E.g.

      www.yoursite.com/events/event?event_start_after=2013-03-01&event_start_before=2013-03-31
    

    will then show events for March 2013. You could then add rewrite rules to make it ‘pretty’.

    Stephen Harris
Viewing 3 posts - 1 through 3 (of 3 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.