Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gchtr/99991dc7ec74af9df7b31845b8c595e3 to your computer and use it in GitHub Desktop.
Save gchtr/99991dc7ec74af9df7b31845b8c595e3 to your computer and use it in GitHub Desktop.
Custom Day of Week filter for "The Events Calendar" plugin that filters events that only start on the selected dates.
<?php
use Tribe\Events\Filterbar\Views\V2\Filters\Day_Of_Week;
/**
* Class Theme_Day_Of_Week_Simple_Filter
*
* @see \Tribe\Events\Filterbar\Views\V2\Filters\Day_Of_Week
* @see \Tribe__Events__Filterbar__Filters__Day_Of_Week
*/
class Theme_Day_Of_Week_Simple_Filter extends Day_Of_Week {
/**
* Updates the where clause to only add a simple DAYOFWEEK() check.
*
* @return void
*/
protected function setup_where_clause() {
/** @var wpdb $wpdb */
global $wpdb;
$values = array_map( 'intval', $this->currentValue );
$values = implode( ',', $values );
$eod_cutoff = tribe_get_option( 'multiDayCutoff', '00:00' );
if ( $eod_cutoff != '00:00' ) {
$eod_time_difference = Tribe__Date_Utils::time_between( '1/1/2014 00:00:00', "1/1/2014 {$eod_cutoff}:00" );
$start_date = "DATE_SUB({$wpdb->postmeta}.meta_value, INTERVAL {$eod_time_difference} SECOND)";
} else {
$start_date = "{$wpdb->postmeta}.meta_value";
}
$this->whereClause = " AND (DAYOFWEEK($start_date) IN ($values))";
// Forces the query to use _EventStartDate and _EventEndDate as the times to base results
// off of, instead of _EventStartDateUTC, _EventEventDateUTC which can produce weird results.
add_filter( 'tribe_events_query_force_local_tz', '__return_true' );
}
}
<?php
require_once __DIR__ . '/class-theme-day-of-week-simple-filter.php';
/**
* Custom Day of Week filter for The Events Calendar plugin that filters events that only start on
* the selected dates.
*
* New filter available in WP-Admin > Events > Settings > Filters
*/
/**
* Includes the custom filter class and creates an instance of it.
*/
add_action( 'tribe_events_filters_create_filters', function() {
new \Theme_Day_Of_Week_Simple_Filter( 'Weekday', 'filterbar_day_of_week_simple' );
} );
/**
* Filters the Context locations to let the Context know how to fetch the value of the filter from
* a request.
*
* @param array<string,array> $locations A map of the locations the Context supports and is able to
* read from and write to.
*
* @return array<string,array> The filtered map of Context locations, with the one required from
* the filter added to it.
*/
add_filter( 'tribe_context_locations', function( array $locations ) {
// Read the filter selected values, if any, from the URL request vars.
$locations['filterbar_day_of_week_simple'] = [
'read' => [
\Tribe__Context::REQUEST_VAR => [ 'tribe_filterbar_day_of_week_simple' ],
],
];
return $locations;
} );
/**
* Filters the map of filters available on the front-end to include the custom one.
*
* @param array<string,string> $map A map relating the filter slugs to their respective classes.
*
* @return array<string,string> The filtered slug to filter class map.
*/
add_filter( 'tribe_events_filter_bar_context_to_filter_map', function( array $map ) {
$map['filterbar_day_of_week_simple'] = Theme_Day_Of_Week_Simple_Filter::class;
return $map;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment