All-in-One Event Calendar: Custom Query – Getting each event Instance

The question:

I am using the All-in-One Event Calendar by Timely, and I would like to display upcoming events from the events calendar in a carousel on a separate page, and I need to get the event information.

I am using the ai1ec helpers and get_events_between to get the events within a certain date range (between today and a year from now), and I can see in the array (from get_events_between that I loop through to get post IDs) that I am getting the correct amount of events (each one is an object).

Then I use a WP query to display the information with the Ai1ec_Events_Helper. I can get the start date, time, etc.

You can see the code here:

However, the problem is that even though the array of events holds 50+ events, I am only getting 7 from my WP query – the 7 individual events I have created, showing the original start date of the event (ie. if an event started March 6 and it repeats once a month, I’m only getting the event once with the date of March 6). The link to each event detail’s page also links to the original date (“?instance_id=” in the URL is empty).

Does anyone know how I can get each instance of each event so that I’m displaying all 50+ events?



This is the output that I get from $get_events for the first array object:

array(56) { [0]=> object(Ai1ec_Event)#389 (51) { [“post”]=> object(stdClass)#388 (23) { [“ID”]=> string(3) “220” [“post_author”]=> string(1) “1” [“post_date”]=> string(19) “2013-03-06 14:00:18” [“post_date_gmt”]=> string(19) “2013-03-06 19:00:18” [“post_content”]=> string(440) “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus nunc dolor, accumsan quis condimentum ut, quis tortor. Consectetur adipiscing elit. Etiam odio purus, imperdiet ac. Duis quis risus a odio sodales bibendum. In adipiscing quam est.” [“post_title”]=> string(17) “Another New Event” [“post_excerpt”]=> string(0) “” [“post_status”]=> string(7) “publish” [“comment_status”]=> string(6) “closed” [“ping_status”]=> string(6) “closed” [“post_password”]=> string(0) “” [“post_name”]=> string(17) “another-new-event” [“to_ping”]=> string(0) “” [“pinged”]=> string(0) “” [“post_modified”]=> string(19) “2013-03-07 14:21:45” [“post_modified_gmt”]=> string(19) “2013-03-07 19:21:45” [“post_content_filtered”]=> string(0) “” [“post_parent”]=> string(1) “0” [“guid”]=> string(94) “…com/wp/?post_type=ai1ec_event&p=220&instance_id=” [“menu_order”]=> string(1) “0” [“post_type”]=> string(11) “ai1ec_event” [“post_mime_type”]=> string(0) “” [“comment_count”]=> string(1) “0” } [“post_id”]=> string(3) “220” [“instance_id”]=> string(3) “582” [“start”]=> int(1363838400) [“end”]=> int(1363924800) [“start_truncated”]=> NULL [“end_truncated”]=> NULL [“allday”]=> string(1) “1” [“instant_event”]=> NULL [“recurrence_rules”]=> string(23) “FREQ=MONTHLY;BYday=3TH;” [“exception_rules”]=> string(0) “” [“recurrence_dates”]=> string(0) “” [“exception_dates”]=> string(0) “” [“venue”]=> string(12) “The location” [“country”]=> string(9) “Australia” [“address”]=> string(44) “The Address, Hope Island QLD 4212, Australia” [“city”]=> string(11) “Hope Island” [“province”]=> string(10) “Queensland” [“postal_code”]=> string(4) “4212” [“show_map”]=> string(1) “1” [“show_coordinates”]=> NULL [“longitude”]=> NULL [“latitude”]=> NULL [“facebook_eid”]=> NULL [“facebook_user”]=> NULL [“facebook_status”]=> NULL [“contact_name”]=> string(12) “Contact Name” [“contact_phone”]=> string(12) “123-456-7890” [“contact_email”]=> string(10) “[email protected]” [“contact_url”]=> NULL [“cost”]=> string(6) “$10.00” [“ticket_url”]=> NULL [“ical_feed_url”]=> string(0) “” [“ical_source_url”]=> string(0) “” [“ical_organizer”]=> NULL [“ical_contact”]=> NULL [“ical_uid”]=> string(0) “” [“tags”]=> NULL [“categories”]=> NULL [“feed”]=> NULL [“category_colors”:”Ai1ec_Event”:private]=> NULL [“color_style”:”Ai1ec_Event”:private]=> NULL [“category_text_color”:”Ai1ec_Event”:private]=> NULL [“category_bg_color”:”Ai1ec_Event”:private]=> NULL [“faded_color”:”Ai1ec_Event”:private]=> NULL [“rgba_color”:”Ai1ec_Event”:private]=> NULL [“tags_html”:”Ai1ec_Event”:private]=> NULL [“category_blocks_html”:”Ai1ec_Event”:private]=> NULL [“category_inline_html”:”Ai1ec_Event”:private]=> NULL [“_is_multiday”:”Ai1ec_Event”:private]=> NULL [“request”:”Ai1ec_Event”:private]=> NULL }

The event just has filler data. As you can see in the foreach loop from the code I linked to, I can get the post ID and instance ID of the events, store them in arrays, and print those out. So I can see it’s grabbing more than 7 events, and the post IDs do duplicate and the instance IDs are unique.

I did try modifying the foreach loop into two to try to iterate through the objects (if that’s way to do it?!), but it would not spit out things like the instance ID, though I could get the post title and guid. Also, in the object it doesn’t have the proper start/end dates of the events ([“start”]=> int(1363838400) [“end”]=> int(1363924800)) so I’m wondering if I have to use the WP query and the Ai1ec_Events_Helper so that I get the dates in the proper format?

ETA 2:

After looking closer to what $get_events is actually giving me, I have modified the foreach loop in the pastebin above to:

foreach($get_events as $event) {
    echo '<p>Event: ' . $event->post->post_title . '<br />';
    echo 'ID: ' . $event->post->ID . '<br />';
    echo 'Instance: ' . $event->instance_id . '<br />';
    echo 'Guid: ' . $event->post->guid . '<br />';
    echo 'Link: <a href="' . $event->post->guid . $event->instance_id . '" rel="nofollow noreferrer noopener">Go to event</a></p>';

I am able to get the event title, ID, instance ID, GUID, and combine the GUID and instance ID to make a proper URL to the correct event, without using the WP query at all. Yay! They also seem to be ordered correctly by date as well (though all-day events display first).

I can get the dates and times of the events as well with $event->start and $event->end and running them through the date() function.

I seem to have answered my own question… I’ll post my final code as an answer once it’s complete.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Finally have a chance to post my solution. Note that the events show All Day events first if there are any. Unfortunately I don’t know a way around this as it is coded this way in a SQL query within one of the plugin files (and it is even commented in the file that All Day events are first).

I am also using the event information collected into image data attributes as it is for a carousel using Cycle2. But you can just change that part to echo out the information or however you want to format it.

I hope this helps someone else if they come across the need to get events from the AI1EC plugin!


    // Gets localized time
    $time = $ai1ec_events_helper->gmt_to_local( Ai1ec_Time_Utility::current_time() );
    $bits = $ai1ec_events_helper->gmgetdate( $time );

    // Sets start time to today
    $start = gmmktime(0,0,0,$bits['mon'],$bits['mday'],$bits['year']);

    // Sets end time to a year from today i.e. $bits['year']+1
    $end = gmmktime(0,0,0,$bits['mon'],$bits['mday'],$bits['year']+1);

    // Look in class-ai1ec-calendar-helper.php for details
    $get_events = $ai1ec_calendar_helper->get_events_between($start, $end, $filter, $spanning = false);

    date_default_timezone_set('America/Toronto'); // Match timezone settings in WP

    // For each event
    foreach($get_events as $event) {

        // Event ID
        $eventID = $event->post->ID;

        // Event Title
        $eventTitle = $event->post->post_title;

        // Event URL
        $eventURL = $event->post->guid . $event->instance_id;

        // Event Date
        $eventMonth = date( 'M', $event->start );
        $eventDay = date( 'd', $event->start );

        // Event Image
        $imgID = get_post_thumbnail_id( $eventID );

        // Event Time
        if( $event->allday == 1 ) {
            $timeSpan = 'All Day';
        } else {
            $startTime = date( 'g:ia', $event->start );
            $endTime = date( 'g:ia', $event->end );
            $timeSpan = $startTime . ' - ' . $endTime;

        $attr = array(
            'alt' => get_post_meta( $imgID , '_wp_attachment_image_alt', true ), 
            'data-name' => $eventTitle, 
            'data-month' => $eventMonth,
            'data-day' => $eventDay,
            'data-time' => $timeSpan,
            'data-url' => $eventURL
        echo get_the_post_thumbnail( $eventID, 'full', $attr );

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment