How to Query Free Events with The Events Calendar
While working a on website for a non profit organization the client requested changes after changes. Among them was a request to segment different categories of events on the home page which include “free events.” Our team first decided to get the URL from the filter plugin. However, because of security concerns, it was not wise to use the URL with long query strings so we had you manually create a code to filter events with “0” cost. After some research via the Tribe website, github, and additional help from Carie Dils post, I was able to write the following code that displays a much cleaner list of free events.
<?php /** * Template Name: STEM Free Events * Description: Used as a page template to show free events. */ // Add our custom loop add_action('genesis_loop', 'display_free_events'); function display_free_events(){ $args = array( 'post_type' => Tribe__Events__Main::POSTTYPE, 'meta_query' => array( array( 'key' => '_EventCost', 'value' => '0', 'compare' => '=', ) ), 'posts_per_page' => -1, ); $free_events = new WP_Query( $args ); if($free_events->have_posts()) : while($free_events->have_posts()) : $free_events->the_post(); ?> <div class="dj-free-events"> <?php echo tribe_event_featured_image( null, 'medium' ); ?> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a><br /> <?php if (tribe_get_start_date() !== tribe_get_end_date() ) { ?> <?php echo tribe_get_start_date(); ?> - <?php echo tribe_get_end_date(); ?> <?php } else { ?> <?php echo tribe_get_start_date(); ?> <?php } ?> <?php the_excerpt(); ?> <?php endwhile; wp_reset_postdata(); ?> <?php else: ?> <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p> <?php endif; } ?> </div> <div class="clearfix"></div> <?php genesis(); ?>
Please let me know if this code is of help to you or if you have any questions.