Skip to content

Instantly share code, notes, and snippets.

@elimn
Last active March 23, 2017 14:54
Show Gist options
  • Save elimn/681e2391f03dfadb8d94e1115bda19ab to your computer and use it in GitHub Desktop.
Save elimn/681e2391f03dfadb8d94e1115bda19ab to your computer and use it in GitHub Desktop.
MT | TEC | Add more display options to the ical feeds
<?php
/**
* Adds more display options to the ical feeds
*
* This will show recently published events, up to 40 of them:
* events-slug/?ical=1&tribe_display=recently-published&tribe_posts_amount=40
*
* This will show all events in the year 2012
* events-slug/?ical=1&tribe_display=year&eventDate=2012
*
* @see pre_get_posts
*/
function tribe_additional_feed_views( $query ) {
if ( ! isset( $_GET['ical'] ) || ! isset( $query->tribe_is_event_query ) || ! $query->tribe_is_event_query ) {
return;
}
$tribe_display_unsanitized = $_GET['tribe_display'];
if ( $tribe_display_unsanitized === 'year' ) {
$which_year = isset( $query->query['eventDate'] ) ? strtotime( $query->query['eventDate'] ) : time();
$start_of_year = date( 'Y', $which_year ) . '-01-01';
$end_of_year = date( 'Y', $which_year ) . '-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 ( 'recently-published' === $tribe_display_unsanitized ) {
$num_posts = isset( $_GET['tribe_posts_amount'] ) ? intval( $_GET['tribe_posts_amount'] ) : 0;
// Uses default if $_GET is not a valid posts_per_page int
if ( $num_posts < -1 ) {
$num_posts = tribe_get_option( 'postsPerPage', 20 );
}
$query->set( 'order', 'DESC' );
$query->set( 'orderby', 'modified' );
$query->set( 'posts_per_page', $num_posts );
// Remove default orderby and wheres, lest they return events sorted by event_date
remove_filter( 'posts_orderby', array( 'Tribe__Events__Query', 'posts_orderby' ), 10 );
remove_filter( 'posts_where', array( 'Tribe__Events__Query', 'posts_where' ), 10 );
}
}
add_action( 'pre_get_posts', 'tribe_additional_feed_views', 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment