Skip to content

Instantly share code, notes, and snippets.

@andrasguseo
Last active November 1, 2017 09:35
Show Gist options
  • Save andrasguseo/158de8d396cd8c6b58e149175bba995c to your computer and use it in GitHub Desktop.
Save andrasguseo/158de8d396cd8c6b58e149175bba995c to your computer and use it in GitHub Desktop.
iCal export of this year's events
<?php
/**
* Description: This snippet will enable you to have an iCal export of all this year's events
* when using this url:
* http://example.com/events/?ical=1&tribe_display=year
*
* Paste the code in your (child) theme's functions.php file
*
* Required plugins: The Events Calendar
* Last updated: 2017-10-20
* Author: Brook Harding
*/
add_action( 'pre_get_posts', 'filter_ical_query' );
function filter_ical_query( $query ) {
if ( ! isset( $_GET['ical'] ) || ! isset( $query->tribe_is_event_query ) || ! $query->tribe_is_event_query ) return;
$tribe_display = $_GET[ 'tribe_display' ];
if( $tribe_display === 'year' ) {
$start_of_year = date( 'Y' ) . '-01-01';
// Use this line to have the events of the current year
$end_of_year = date( 'Y' ) . '-12-31';
// Use this line to have the events until the specified date
// $end_of_year = '2020-12-31';
$query->set( 'eventDisplay', 'custom' );
$query->set( 'start_date', $start_of_year );
$query->set( 'end_date', $end_of_year );
$query->set( 'posts_per_page', - 1 );
}
elseif ( $tribe_display === 'recently-published' ) {
$query->set( 'order', 'DESC' );
$query->set( 'orderby', 'modified' );
$query->set( 'posts_per_page', tribe_get_option( 'postsPerPage', 20 ) );
// Remove default oderby and wheres, lest they return events sorted by event_date
remove_filter( 'posts_orderby', array( 'Tribe__Events__Query', 'posts_orderby' ), 10, 2 );
remove_filter( 'posts_where', array( 'Tribe__Events__Query', 'posts_where' ), 10, 2 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment