Wp Event Aggregator Shortcode

Below, you’ll find a detailed guide on how to add the Wp Event Aggregator Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Wp Event Aggregator Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Wp Event Aggregator Plugin and the shortcodes it provides:

Plugin Icon
WP Event Aggregator: Import Eventbrite events, Meetup events, social events and any iCal Events into WordPress

"WP Event Aggregator is a comprehensive plugin for importing events from various platforms such as Eventbrite, Meetup, and any iCal events into your WordPress site."

★★★★☆ (50) Active Installs: 2000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [wp_events]

Wp Event Aggregator [wp_events] Shortcode

The wp-event-aggregator shortcode allows users to display a list of events on their WordPress site. It supports various attributes to customize the output, such as the number of events per page, specific categories, and date ranges.

Shortcode: [wp_events]

Parameters

Here is a list of all possible wp_events shortcode parameters and attributes:

  • col – determines the number of columns in the layout
  • layout – sets the style of the event listing
  • posts_per_page – sets the number of events displayed per page
  • category – filters events by specified categories
  • past_events – allows display of past events
  • order – sets the order of events, ascending or descending
  • orderby – determines the parameter for ordering events
  • start_date – filters events that start after the specified date
  • end_date – filters events that end before the specified date

Examples and Usage

Basic example – Display events with default settings

[wp_events]

Advanced examples

Display events from specific categories (for example, “cat1” and “cat2”).

[wp_events category="cat1,cat2"]

Display 12 events per page in two columns.

[wp_events posts_per_page='12' col='2']

Show past events in descending order.

[wp_events past_events="yes" order="desc"]

Display events happening between specific dates (for example, between 1st January 2022 and 31st December 2022).

[wp_events start_date="2022-01-01" end_date="2022-12-31"]

Show events in a specific layout (for example, “style2”).

[wp_events layout="style2"]

Customize the order of events by a specific parameter (for example, “event_start_date”).

[wp_events orderby="event_start_date"]

PHP Function Code

In case you have difficulties debugging what causing issues with [wp_events] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('wp_events', array( $this, 'wp_events_archive' ) );

Shortcode PHP function:

function wp_events_archive( $atts = array() ){
		//[wp_events col='2' layout="style2" posts_per_page='12' category="cat1,cat2" past_events="yes" order="desc" orderby="" start_date="" end_date="" ]
		$current_date = current_time('timestamp');
		$paged = ( get_query_var('paged') ? get_query_var('paged') : 1 );
		if( is_front_page() ){
			$paged = ( get_query_var('page') ? get_query_var('page') : 1 );
		}
		$eve_args = array(
		    'post_type' => 'wp_events',
		    'post_status' => 'publish',
		    'meta_key' => 'start_ts',
		    'paged' => $paged,
		);

		// post per page.
		if( isset( $atts['posts_per_page'] ) && $atts['posts_per_page'] != '' && is_numeric( $atts['posts_per_page'] ) ){
			$eve_args['posts_per_page'] = $atts['posts_per_page'];
		}

		// Past Events
		if( ( isset( $atts['start_date'] ) && $atts['start_date'] != '' ) || ( isset( $atts['end_date'] ) && $atts['end_date'] != '') ){
			$start_date_str = $end_date_str = '';
			if( isset( $atts['start_date'] ) && $atts['start_date'] != '' ){
				try {
				    $start_date_str = strtotime( sanitize_text_field( $atts['start_date'] ));
				} catch (Exception $e) {
					$start_date_str = '';
				}	
			}
			if( isset( $atts['end_date'] ) && $atts['end_date'] != '' ){
				try {
				    $end_date_str = strtotime( sanitize_text_field( $atts['end_date'] ));
				} catch (Exception $e) {
					$end_date_str = '';
				}
			}

			
			if( $start_date_str != '' && $end_date_str != '' ){
				$eve_args['meta_query'] = array(
						   'relation' => 'AND',                        
					        array(
					            'key' => 'end_ts',
					            'compare' => '>=',
					            'value' => $start_date_str,
					        ),
					        array(
					            'key' => 'end_ts',
					            'compare' => '<=',
					            'value' => $end_date_str,
					        ),
				        );
			}elseif(  $start_date_str != '' ){
				$eve_args['meta_query'] = array(
					        array(
					            'key' => 'end_ts',
					            'compare' => '>=',
					            'value' => $start_date_str,
					        )
				        );
			}elseif(  $end_date_str != '' ){
				$eve_args['meta_query'] = array(
						   'relation' => 'AND',                        
					        array(
					            'key' => 'end_ts',
					            'compare' => '>=',
					            'value' => strtotime( date( 'Y-m-d' ) ),
					        ),
					        array(
					            'key' => 'end_ts',
					            'compare' => '<=',
					            'value' => $end_date_str,
					        ),
				        );
			}

		}else{
			if( isset( $atts['past_events'] ) && $atts['past_events'] == true ){
				$atts['past_events'] = "yes";
			}
			if( isset( $atts['past_events'] ) && $atts['past_events'] == 'yes' ){
				$eve_args['meta_query'] = array(
						        array(
						            'key' => 'end_ts',
						            'compare' => '<=',
						            'value' => $current_date,
						        )
				            );
			}else{
				$eve_args['meta_query'] = array(
						        array(
						            'key' => 'end_ts',
						            'compare' => '>=',
						            'value' => $current_date,
						        )
				            );
			}
		}
		
		if( isset( $atts['category'] ) && $atts['category'] != '' ){
			$categories = explode(',', $atts['category'] );
			$tax_field = 'slug';
			if( is_numeric( implode('', $categories ) ) ){
				$tax_field = 'term_id';
			}
			if( !empty( $categories ) ){
				$eve_args['tax_query'] = array(
					array(
						'taxonomy' => $this->event_category,
						'field'    => $tax_field,
						'terms'    => $categories,
					)
				);
			}
		}

		// Order by
		if( isset( $atts['orderby'] ) && $atts['orderby'] != '' ){
			if( $atts['orderby'] == 'event_start_date' || $atts['orderby'] == 'event_end_date' ){
				$eve_args['orderby'] = 'meta_value';
			}else{
				$eve_args['orderby'] = sanitize_text_field( $atts['orderby'] );
			}
		}else{
			$eve_args['orderby'] = 'meta_value';
		}

		// Order
		if( isset( $atts['order'] ) && $atts['order'] != '' ){
			if( strtoupper( $atts['order'] ) == 'DESC' || strtoupper( $atts['order'] ) == 'ASC' ){
				$eve_args['order'] = sanitize_text_field( $atts['order'] );
			}
		}else{
			if( isset( $atts['past_events'] ) && $atts['past_events'] == true ){
				$atts['past_events'] = "yes";
			}
			if( isset( $atts['past_events'] ) && $atts['past_events'] == 'yes' && $eve_args['orderby'] == 'meta_value' ){
				$eve_args['order'] = 'DESC';
			}else{
				$eve_args['order'] = 'ASC';
			}
		}

		$col = 2;
        $css_class = 'col-wpea-md-6';
        if( isset( $atts['col'] ) && $atts['col'] != '' && is_numeric( $atts['col'] ) ){
             $col = $atts['col'];
                switch ( $col ) {
                case '1':
                    $css_class = 'col-wpea-md-12';
                    break;

                case '2':
                    $css_class = 'col-wpea-md-6';
                    break;

                case '3':
                    $css_class = 'col-wpea-md-4';
                    break;

                case '4':
                    $css_class = 'col-wpea-md-3';
                    break;
               
                default:
                    $css_class = 'col-wpea-md-4';
                    break;
            }
        }  

        $wp_events = new WP_Query( $eve_args );
		
		$wp_list_events = '';
		/* Start the Loop */
		if( is_front_page() ){
			$curr_paged = $paged;
			global $paged;
			$temp_paged = $paged;
			$paged = $curr_paged;
        }
		$wpea_options = get_option( WPEA_OPTIONS );
		$accent_color = isset( $wpea_options['wpea']['accent_color'] ) ? $wpea_options['wpea']['accent_color'] : '#039ED7';
        $direct_link = isset($wpea_options['wpea']['direct_link']) ? $wpea_options['wpea']['direct_link'] : 'no';
        if (!wpea_is_pro()) {
            $direct_link = 'no';
        }
		ob_start();
		?>
		<div class="row_grid wpea_frontend_archive">
			<?php
			$template_args = array();
            $template_args['css_class'] = $css_class;
            $template_args['direct_link'] = $direct_link;
			if( $wp_events->have_posts() ):
				while ( $wp_events->have_posts() ) : $wp_events->the_post();
					
					if( isset( $atts['layout'] ) && $atts['layout'] == 'style2' && wpea_is_pro() ){
						get_wpea_template( 'wpea-archive-content2.php', $template_args );
					}else{
						get_wpea_template( 'wpea-archive-content.php', $template_args );
					}
					
				endwhile; // End of the loop.

				if ($wp_events->max_num_pages > 1) : // custom pagination  ?>
					<div class="col-wpea-md-12">
						<nav class="prev-next-posts">
							<div class="prev-posts-link alignright">
								<?php echo get_next_posts_link( 'Next Events &raquo;', $wp_events->max_num_pages ); ?>
							</div>
							<div class="next-posts-link alignleft">
								<?php echo get_previous_posts_link( '&laquo; Previous Events' ); ?>
							</div>
						</nav>
					</div>
				<?php endif;
			else:
				echo apply_filters( 'wpea_no_events_found_message', __( "No Events are found.", 'wp-event-aggregator' ) );
			endif;

			?>
		</div>
		<style type="text/css">
			.wpea_frontend_archive .event_date{
			    background-color: <?php echo $accent_color;?>;
			}
			.wpea_frontend_archive .event_desc .event_title, .wpea_frontend_archive .event_desc a{
			    color: <?php echo $accent_color;?>;
			}
		</style>
		<?php
		do_action( 'wpea_after_event_list', $wp_events );
		$wp_list_events = ob_get_contents();
		ob_end_clean();
		wp_reset_postdata();
		if( is_front_page() ){
			global $paged;
			$paged = $temp_paged;
		}
		return $wp_list_events;

	}

Code file location:

wp-event-aggregator/wp-event-aggregator/includes/class-wp-event-aggregator-cpt.php

Conclusion

Now that you’ve learned how to embed the Wp Event Aggregator Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *