Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AI1EC usage sample
function ai1ec2_display_events_func( $atts ) {
global $ai1ec_registry;
$content = '<div id="content2"><img src="/wp-content/themes/scio/images/events-header.png" alt="" />';
$time = $ai1ec_registry->get( 'date.system' );
// Get localized time
$timestamp = $time->current_time();
// Set $limit to the specified categories/tags
$limit = array(
//
// this is demo data - please use your own filters
//
'tag_ids' => array ( 29 )
);
$events_per_page = 2;
$paged = 0;
$event_results = $ai1ec_registry->get( 'model.search' )
->get_events_relative_to(
$timestamp,
$events_per_page,
$paged,
$limit
);
$dates = $ai1ec_registry->get(
'view.calendar.view.agenda',
$ai1ec_registry->get( 'http.request.parser' )
)->get_agenda_like_date_array( $event_results['events'] );
foreach ( $dates as $date ) {
foreach ( $date['events']['allday'] as $instance ) {
$post_title = $instance->get( 'post' )->post_title;
$post_name = $instance->get( 'post' )->post_name;
$post_content = $instance->get( 'post' )->post_content;
$instance_id = $instance->get( 'instance_id' );
$content .= '<div class="hp-event">
<a href="ai1ec_event/'.$post_name.'/?instance_id='.$instance_id.'">'
.$post_title.'</a><br />'
.$post_content.
'<a class="read-more-link" href="ai1ec_event/'.$post_name.'/?instance_id='.$instance_id.'">read more</a>
</div>';
}
foreach ( $date['events']['notallday'] as $instance ) {
$post_title = $instance->get( 'post' )->post_title;
$post_name = $instance->get( 'post' )->post_name;
$post_content = $instance->get( 'post' )->post_content;
$instance_id = $instance->get( 'instance_id' );
$content .= '<div class="hp-event">
<a href="ai1ec_event/'.$post_name.'/?instance_id='.$instance_id.'">'
.$post_title.'</a><br />'
.$post_content.
'<a class="read-more-link" href="ai1ec_event/'.$post_name.'/?instance_id='.$instance_id.'">read more</a>
</div>';
}
}
$content .= '</div>';
return $content;
}
add_shortcode( 'display_events2', 'ai1ec2_display_events_func' );
@ericnkatz

This comment has been minimized.

Copy link

commented Jun 25, 2014

Thanks for this. :)

@lukaspawlik

This comment has been minimized.

Copy link
Owner Author

commented Jun 28, 2014

I am glad that I could help ;)

@brianfeister

This comment has been minimized.

Copy link

commented Jul 6, 2014

@lukaspawlik - can you let me know which version of the ai1ec plugin you're running. I'm on 2.0, maybe this code is 2.1? Unfortunately not working for me.

This bit

  $event_results   = $ai1ec_registry->get( 'model.search' )->get_events_relative_to(
      $timestamp,
      $events_per_page,
      $paged,
      $limit
    );

Is returning a huge registry object, however, the following is returning empty (even though I have events):

  $dates = $ai1ec_registry->get(
      'view.calendar.view.agenda',
      $ai1ec_registry->get( 'http.request.parser' )
    )->get_agenda_like_date_array( $event_results['events'] );
@brianfeister

This comment has been minimized.

Copy link

commented Jul 6, 2014

My mistake, the 'tag_ids' => array ( 29 ) was specific to your code and that threw me off. I would recommend removing that from your gist so as to avoid confusion for others reading this ;)

@lukaspawlik

This comment has been minimized.

Copy link
Owner Author

commented Jul 8, 2014

Hi @brianfeister,
it was prepared for 2.0 - this code is rewritten example from Timely support forum and as you noticed this 'tag_ids' => array( 29 ) is specific to source example. I think that now it's good to remove it ;) as you said. Thanks for pointing that.

@angelwizz

This comment has been minimized.

Copy link

commented Jul 18, 2014

Thank you for this code Lukas.. I need to be able to get a little deeper into the array .. How do you access more information like the venue,start and end date

@lukaspawlik

This comment has been minimized.

Copy link
Owner Author

commented Jul 23, 2014

@angelwizz please take a look at list of available properties - https://gist.github.com/lukaspawlik/c4a0e605414542e844dd - you can access it via
$instance->get( 'start' );
$instance->get( 'end' );
$instance->get( 'venue' );

Please be aware that start and end are not just DateTime - they are instances of Ai1ec_Date_Time.

Sorry for late response - github doesn't notify me about comments.

@alexandar87

This comment has been minimized.

Copy link

commented Aug 22, 2014

Hey, can this help me to get date and location in my query?

Or at least point me how can i use code from this post, i paste it in my functions.php and than do the shortcode, but than again if i dont get any content its because filters doesnt match...

This is my pretty basic query, and i need just to get date and location in it.

 <?php
                                $type = 'ai1ec_event';
                                $args = array(
                                    'post_type' => $type,
                                    'showposts' => 2,
                                    'order' => 'ASC'
                                );
                                $my_query = new WP_Query($args);
                                if ($my_query->have_posts()) {
                                    while ($my_query->have_posts()) : $my_query->the_post(); ?>

                                        <h3 class="no-mar fc-second"><?php the_title(); ?></h3>

                                        <p><?php the_content(); ?></p>
                                    <?php
                                    endwhile;
                                }
                                wp_reset_query(); 
                                ?>
@angelwizz

This comment has been minimized.

Copy link

commented Aug 29, 2014

Hey Lucas,

Loving this code but I need your help.. I have a site which needs to show a different event category on different pages.. If I change the tag_ID using the code above it doesn't change the event content shown.. Basically it doesn't matter what tag_ID I put in it stays the same as the first or main category.. How do you add different categories to different pages..
Please can you help me

@lukaspawlik

This comment has been minimized.

Copy link
Owner Author

commented Sep 2, 2014

Hi @ul71m0,
you should not use WP_Query. Events are stored in wp_post only partialy. Rest of informations is stored in custom tables like ai1ec_events and ai1ec_event_instances. You won't be able to obtain date as it is stored in mentioned ai1ec_event_instances (and date is related to event timezone etc.). Events can be also recurring and this complicates things a little. I would start with

global $ai1ec_registry;
$time            = $ai1ec_registry->get( 'date.system' );
$timestamp       = $time->current_time();
$event_results   = $ai1ec_registry->get( 'model.search' )
        ->get_events_relative_to(
            $timestamp,
            $events_per_page,
            $paged,
            $limit
        );

Then you have $event_results as an array of Ai1ec_Event (you can check one of my gists to see what properties are available).
Please not that I didn't define $limit variable - it is array with any combination of below arguments

     *                                   ['cat_ids']  => list of category IDs;
     *                                   ['tag_ids']  => list of tag IDs;
     *                                   ['post_ids'] => list of post IDs;
     *                                   ['auth_ids'] => list of author IDs.

so

$limit = array(
    'cat_ids' => array( 1, 2, 3 ),
    'post_ids' => array( 1, 2, 3 )
);

Cats/tags are taxonomies so it should not be difficult to get their ids. Post_ids are normal wp_posts IDs but please remember that recurring events are not mapped 1:1 to wp_posts.
Sorry for late response but it's hard to track gist comments without any notifications.

Lukas

@lukaspawlik

This comment has been minimized.

Copy link
Owner Author

commented Sep 2, 2014

Hi @angelwizz,
please read my above comment -tag_ids is an example for forum purposes. In your case you should pass cat_ids instead of tag_ids (you can combine them as you wish).

Lukas

@angelwizz

This comment has been minimized.

Copy link

commented Sep 3, 2014

Thanks Lukas

You are so helpful.. Have an amazing day :)

@petrab

This comment has been minimized.

Copy link

commented Jan 29, 2015

Hello Lukas,
thank you for this code. I'm glad I found it. However, the timestamp seems to be ignored. It always shows me only future events, even when I change the timestamp to the past. Maybe my understanding of the plugin functions is to limited. In case you have any pointers for me how to get the code to show also past events, I would be grateful for them. In any case, thank you very much for your work.
Kind regards, Petra

@karissademi

This comment has been minimized.

Copy link

commented Jun 8, 2015

This gist is one of the most helpful things on the internet, ever. Thank you so much.

@somenamethatisnttaken

This comment has been minimized.

Copy link

commented Nov 19, 2015

Thanks for posting this. I'm having issues with the $post_title = $instance->get( 'post' )->post_title; part of this. Fatal error: Call to a member function get() on a non-object.

I've done a dump on $instance and i see all values I need but can't seem to call them.

Thoughts?

@Susa38

This comment has been minimized.

Copy link

commented Jan 26, 2016

thanks a lot.
One question.
Is not possible to show the post feature image?

@nelero

This comment has been minimized.

Copy link

commented Dec 12, 2016

@somenamethatisnttaken : it seems that for now, it's not an object but an array which is returned by A1ec.
So i had to add $post = get_post($instance['post_id']);
and replace $instance->get( 'post' ) by $post for code after

@gregthrive

This comment has been minimized.

Copy link

commented Mar 8, 2017

@nelero - Can you elaborate on your comment? I'm not quite following-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.