Tribe Events Calendar – Custom Calendar Start

Tribe Events Calendar is a great plugin, that has a lot of very simple queries you can use to create calendar feeds within your templates.

If you have an event on your website, and a calendar that’s launching before weeks (or even months) before the event is taking place, you can use a function to setup a start date for your calendar. The Calendar’s default page views (month, day, list) will load starting from the date entered in the function.

Custom Calendar Start:

Copy and paste this snippet into your functions file of your theme.

/**
 * Sets the default date for event queries.
 *
 * Expects to be called during tribe_events_pre_get_posts. Note that this
 * function modifies $_REQUEST - this is needed for consistency because
 * various parts of TEC inspect that array directly to determine the current
 * date.
 * 
 * @param WP_Query $query
 */
function tribe_force_event_date( WP_Query $query ) {
    // Don't touch single posts or queries other than the main query
    if ( ! $query->is_main_query() || is_single() ) {
        return;
    }

    // If a date has already been set by some other means, bail out
    if ( strlen( $query->get( 'eventDate' ) ) || ! empty( $_REQUEST['tribe-bar-date'] ) ) {
        return;
    }

    // Change this to whatever date you prefer
    $default_date = '2015-10-01'; // update this date field

    // Use the preferred default date
    $query->set( 'eventDate', $default_date );
    $query->set( 'start_date', $default_date );
    $_REQUEST['tribe-bar-date'] = $default_date;
}

add_action( 'tribe_events_pre_get_posts', 'tribe_force_event_date' );

Sourced here.

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.