Skip to content

Instantly share code, notes, and snippets.

@elimn
Last active April 19, 2022 13:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save elimn/d034dfddb9be206d9cc1 to your computer and use it in GitHub Desktop.
Save elimn/d034dfddb9be206d9cc1 to your computer and use it in GitHub Desktop.
MT | TEC | Set the default date for views like List and Month
<?php
/*
* Set the default date for views like List and Month
* Modify the $date variable below to set your date
*/
function tribe_set_default_date( $wp_query ) {
// Only run on main event queries
if ( ! tribe_is_event_query() || ! $wp_query->is_main_query() || $wp_query->get( 'eventDate' ) != '') return;
// Set this to the date you want shown, in YYYY-MM-DD format
$date = '2000-01-01';
// Select which TEC views you want this to apply to by uncommenting them
if (
tribe_is_list_view()
// || tribe_is_map()
// || tribe_is_photo()
// || tribe_is_month()
// || tribe_is_week()
// || tribe_is_day()
) {
$wp_query->set( 'eventDate', $date );
}
}
if ( function_exists('tribe_is_event_query') ) {
add_action( 'parse_query', 'tribe_set_default_date', 100);
}
/*
* Sets the date search value to never be blank
*/
function tribe_bar_set_date_val( $filters ) {
global $wp_query;
$new_val = 'value="' . esc_attr( $wp_query->get( 'eventDate' ) ) . '"';
$filters['tribe-bar-date']['html'] = preg_replace( '/value="[^"]*"/', $new_val, $filters['tribe-bar-date']['html'] );
return $filters;
}
add_filter( 'tribe-events-bar-filters', 'tribe_bar_set_date_val', 100, 1 );
@calebfergie
Copy link

Hi there, thanks so much for this.

Our events page is almost at where we need - check it out here- I took a look at the Themer's Guide but can't seem to figure out how to:

  1. Change the title from "Upcoming Events" to something else (e.g. "BlockApps Events")
  2. Change the order of the events to most-recent first (is currently least-recent first)

Could you tell us how to do so? I am comfortable manipulating PHP but don't know really how to code for it. We also use Avada if that is relevant to the solution.

@andrebarretocubed
Copy link

Hi there, thanks so much for this.

Our events page is almost at where we need - check it out here- I took a look at the Themer's Guide but can't seem to figure out how to:

  1. Change the title from "Upcoming Events" to something else (e.g. "BlockApps Events")
  2. Change the order of the events to most-recent first (is currently least-recent first)

Could you tell us how to do so? I am comfortable manipulating PHP but don't know really how to code for it. We also use Avada if that is relevant to the solution.

Hello! I just checked out the site you linked, Im wondering how you got all the events to show in reverse pagination. I can only get them to do that when I have a long list of 100+ events per page.

Any help would be appreciated!

@calebfergie
Copy link

Hello @andrebarretocubed - I’ve sort of lost track of which events code does what for our site, but here’s everything we’ve tried - maybe your answer is in here. Sorry for the poor formatting, I’m on my mobile!

‘’’

/* Show all events by default - solution from https://theeventscalendar.com/support/forums/topic/view-all-events-list// /
/

  • Set the default date for views like List and Month
  • Modify the $date variable below to set your date
    /
    function tribe_set_default_date( $wp_query ) {
    // Only run on main event queries
    if ( ! tribe_is_event_query() || ! $wp_query->is_main_query() || $wp_query->get( 'eventDate' ) != '') return;
    // Set this to the date you want shown, in YYYY-MM-DD format
    $date = '2019-09-14';
    // Select which TEC views you want this to apply to by uncommenting them
    if (
    tribe_is_list_view()
    // || tribe_is_map()
    // || tribe_is_photo()
    // || tribe_is_month()
    // || tribe_is_week()
    // || tribe_is_day()
    ) {
    $wp_query->set( 'eventDate', $date );
    }
    }
    if ( function_exists('tribe_is_event_query') ) {
    add_action( 'parse_query', 'tribe_set_default_date', 100);
    }
    /
  • Sets the date search value to never be blank
    */

function tribe_bar_set_date_val( $filters ) {
global $wp_query;
$new_val = 'value="' . esc_attr( $wp_query->get( 'eventDate' ) ) . '"';
$filters['tribe-bar-date']['html'] = preg_replace( '/value="[^"]*"/', $new_val, $filters['tribe-bar-date']['html'] );
return $filters;
}
add_filter( 'tribe-events-bar-filters', 'tribe_bar_set_date_val', 100, 1 );

/* Hide the "event has passed" popup - solution from https://gist.github.com/geoffgraham/e297c0ecd141705d0e2637bfe78202d6/ */
add_filter( 'tribe_the_notices', 'customize_notice', 10, 2 );
function customize_notice( $html, $notices ) {
// If text is found in notice, then replace it
if (tribe_event_in_category('conference') && stristr( $html, 'This event has passed.' ) ) {
// Customize the message as needed
$html = str_replace( '', $html );
}

return $html;

}

/*

  • Change events page titles solution from https://support.theeventscalendar.com/435435-Altering-or-removing-titles-on-calendar-views
    */
    function tribe_alter_event_archive_titles ( $original_recipe_title, $depth ) {
    // Modify the titles here
    // Some of these include %1$s and %2$s, these will be replaced with relevant dates
    $title_upcoming = 'Events We're Attending'; // List View: Upcoming events
    $title_past = 'Events We're Attending'; // List view: Past events
    $title_range = 'Events for %1$s - %2$s'; // List view: range of dates being viewed
    $title_month = 'Events for %1$s'; // Month View, %1$s = the name of the month
    $title_day = 'Events for %1$s'; // Day View, %1$s = the day
    $title_all = 'All events for %s'; // showing all recurrences of an event, %s = event title
    $title_week = 'Events for week of %s'; // Week view
    // Don't modify anything below this unless you know what it does
    global $wp_query;
    $tribe_ecp = Tribe__Events__Main::instance();
    $date_format = apply_filters( 'tribe_events_pro_page_title_date_format', tribe_get_date_format( true ) );
    // Default Title
    $title = $title_upcoming;
    // If there's a date selected in the tribe bar, show the date range of the currently showing events
    if ( isset( $_REQUEST['tribe-bar-date'] ) && $wp_query->have_posts() ) {
    if ( $wp_query->get( 'paged' ) > 1 ) {
    // if we're on page 1, show the selected tribe-bar-date as the first date in the range
    $first_event_date = tribe_get_start_date( $wp_query->posts[0], false );
    } else {
    //otherwise show the start date of the first event in the results
    $first_event_date = tribe_event_format_date( $_REQUEST['tribe-bar-date'], false );
    }
    $last_event_date = tribe_get_end_date( $wp_query->posts[ count( $wp_query->posts ) - 1 ], false );
    $title = sprintf( $title_range, $first_event_date, $last_event_date );
    } elseif ( tribe_is_past() ) {
    $title = $title_past;
    }
    // Month view title
    if ( tribe_is_month() ) {
    $title = sprintf(
    $title_month,
    date_i18n( tribe_get_option( 'monthAndYearFormat', 'F Y' ), strtotime( tribe_get_month_view_date() ) )
    );
    }
    // Day view title
    if ( tribe_is_day() ) {
    $title = sprintf(
    $title_day,
    date_i18n( tribe_get_date_format( true ), strtotime( $wp_query->get( 'start_date' ) ) )
    );
    }
    // All recurrences of an event
    if ( function_exists('tribe_is_showing_all') && tribe_is_showing_all() ) {
    $title = sprintf( $title_all, get_the_title() );
    }
    // Week view title
    if ( function_exists('tribe_is_week') && tribe_is_week() ) {
    $title = sprintf(
    $title_week,
    date_i18n( $date_format, strtotime( tribe_get_first_week_day( $wp_query->get( 'start_date' ) ) ) )
    );
    }
    if ( is_tax( $tribe_ecp->get_event_taxonomy() ) && $depth ) {
    $cat = get_queried_object();
    $title = '' . $title . '';
    $title .= ' › ' . $cat->name;
    }
    return $title;
    }
    add_filter( 'tribe_get_events_title', 'tribe_alter_event_archive_titles', 11, 2 );
    ‘’’

@andrebarretocubed
Copy link

Thanks so much! I appreciate the help!

@KooBa777
Copy link

KooBa777 commented Feb 11, 2021

Hi Guys, I'm not much in to coding, could you tell me where I can find the file that the above code suppose to be added?

@calebfergie
Copy link

@KooBa777 I bet you've already figured it out but - this code can be added in directly to the functions.php file that you can access through your WP dashboard: Appearance > Theme Editor > function.php on the right

@KooBa777
Copy link

@calebfergie Thanks a lot for the reply. What else should be done after pasting the code to function.php? It doesn't seem to work for me at the moment.

@calebfergie
Copy link

@KooBa777 everyones wordpress is a little different. Here’s all i know for sure:

  • make sure you hit save/update at the bottom of the sceeen
  • Confirm you are changing the functions.php file in your ACTIVE theme - change this in the top right corner with the dropdown menu.

Let me know how it goes!

@aradzhioni
Copy link

Hi there, thanks so much for this.

Our events page is almost at where we need - check it out here- I took a look at the Themer's Guide but can't seem to figure out how to:

  1. Change the title from "Upcoming Events" to something else (e.g. "BlockApps Events")
  2. Change the order of the events to most-recent first (is currently least-recent first)

Could you tell us how to do so? I am comfortable manipulating PHP but don't know really how to code for it. We also use Avada if that is relevant to the solution.

As I can see your page is perfect now.

I tried your code, but I can only get all the events on one page. No reverse pagination, No separation between past and upcoming events.
Can you please tell me did you made any changes in the code that you have sent here, or maybe some other things you did to do it the way to shows now?

Thank you in advance!

@calebfergie
Copy link

As I can see your page is perfect now.

I tried your code, but I can only get all the events on one page. No reverse pagination, No separation between past and upcoming events. Can you please tell me did you made any changes in the code that you have sent here, or maybe some other things you did to do it the way to shows now?

Thank you in advance!

hi @aradzhioni - I'm sorry, but I don't work at BlockApps anymore and no longer have access to the wordpress 😞

Good luck with it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment