Title: Next Event Shortcode
Last modified: August 30, 2016

---

# Next Event Shortcode

 *  Resolved [Eric Klingensmith](https://wordpress.org/support/users/eric_thread/)
 * (@eric_thread)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/next-event-shortcode/)
 * I noticed a lot of people asking this question and no easy solution, so after
   completing this I figured I’d post it up for everyone. This pretty much functions
   the same way as the native [event] shortcode except for the fact that instead
   of looking for an event by ID, it simply pulls the next event. I’ll probably 
   further expand on this with filtering, but for now, here we go:
 *     ```
       <?php
       /**
        * Shows the next event according to given specifications. Accepts any event query attribute.
        * @param array $atts
        * @return string
        */
       function em_get_next_event_shortcode($atts, $format='') {
           global $EM_Event, $post;
   
           // Get the first event in the list
           $events = EM_Events::get( array('limit'=>1) ); // It's an array at this point
   
           // Make sure we got something
           if( !empty($events) ) {
               // Now set the global
               $EM_Event = $events[0];
           }            
   
           // Now continue just like the originaL
           $return = '';
           $the_event = is_object($EM_Event) ? clone($EM_Event):null; //save global temporarily
           $atts = (array) $atts;
           $atts['format'] = ($format != '' || empty($atts['format'])) ? $format : $atts['format'];
           $atts['format'] = html_entity_decode($atts['format']); //shorcode doesn't accept html
           if( !empty($atts['event']) && is_numeric($atts['event']) ){
               $EM_Event = em_get_event($atts['event']);
               $return = ( !empty($atts['format']) ) ? $EM_Event->output($atts['format']) : $EM_Event->output_single();
           }elseif( !empty($atts['post_id']) && is_numeric($atts['post_id']) ){
               $EM_Event = em_get_event($atts['post_id'], 'post_id');
               $return = ( !empty($atts['format']) ) ? $EM_Event->output($atts['format']) : $EM_Event->output_single();
           }
           //no specific event or post id supplied, check globals
           if( !empty($EM_Event) ){
               $return = ( !empty($atts['format']) ) ? $EM_Event->output($atts['format']) : $EM_Event->output_single();
           }elseif( $post->post_type == EM_POST_TYPE_EVENT ){
               $EM_Event = em_get_event($post->ID, 'post_id');
               $return = ( !empty($atts['format']) ) ? $EM_Event->output($atts['format']) : $EM_Event->output_single();
           }
           $EM_Event = is_object($the_event) ? $the_event:$EM_Event; //reset global
           return $return;
       }
       add_shortcode ( 'next_event', 'em_get_next_event_shortcode' );
       ?>
       ```
   
 * [https://wordpress.org/plugins/events-manager/](https://wordpress.org/plugins/events-manager/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [caimin_nwl](https://wordpress.org/support/users/caimin_nwl/)
 * (@caimin_nwl)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/next-event-shortcode/#post-6324739)
 * Thanks for sharing – should help someone else.
 *  Thread Starter [Eric Klingensmith](https://wordpress.org/support/users/eric_thread/)
 * (@eric_thread)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/next-event-shortcode/#post-6324741)
 * I hope it does. Lord knows the WP community has helped me quite significantly
   over the years, so much so that I’m finally to a point where I can contribute
   in return. So I figure it’s about time that I actively make an effort toward 
   giving back.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Next Event Shortcode’ is closed to new replies.

 * ![](https://ps.w.org/events-manager/assets/icon-256x256.png?rev=3550347)
 * [Events Manager - Calendar, Bookings, Tickets, and more!](https://wordpress.org/plugins/events-manager/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/events-manager/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/events-manager/)
 * [Active Topics](https://wordpress.org/support/plugin/events-manager/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/events-manager/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/events-manager/reviews/)

## Tags

 * [next event](https://wordpress.org/support/topic-tag/next-event/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * 2 replies
 * 2 participants
 * Last reply from: [Eric Klingensmith](https://wordpress.org/support/users/eric_thread/)
 * Last activity: [10 years, 10 months ago](https://wordpress.org/support/topic/next-event-shortcode/#post-6324741)
 * Status: resolved