Skip to content

Instantly share code, notes, and snippets.

@rliverman
Created August 26, 2014 20:27
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save rliverman/f6ee4d51126a005a66ea to your computer and use it in GitHub Desktop.
Save rliverman/f6ee4d51126a005a66ea to your computer and use it in GitHub Desktop.
Add a custom filter to "The Events Calendar Pro" filter bar plugin based on a taxonomy
<?php
/**
* Adding a custom filter
* A simple copy and paste of the existing Category filter
* Find this new filter in: WP Admin > Events > Settings > Filterbar
* Docs for TribeEventsFilter: http://docs.tri.be/Filter-Bar/class-TribeEventsFilter.html
*/
class TribeEventsFilter_CustomClubs extends TribeEventsFilter {
public $type = 'select';
public function get_admin_form() {
$title = $this->get_title_field();
$type = $this->get_type_field();
return $title.$type;
}
protected function get_type_field() {
$name = $this->get_admin_field_name('type');
$field = sprintf( __( 'Type: %s %s', 'tribe-events-filter-view' ),
sprintf( '<label><input type="radio" name="%s" value="select" %s /> %s</label>',
$name,
checked( $this->type, 'select', false ),
__( 'Dropdown', 'tribe-events-filter-view' )
),
sprintf( '<label><input type="radio" name="%s" value="checkbox" %s /> %s</label>',
$name,
checked( $this->type, 'checkbox', false ),
__( 'Checkboxes', 'tribe-events-filter-view' )
)
);
return '<div class="tribe_events_active_filter_type_options">'.$field.'</div>';
}
protected function get_values() {
$event_clubs = array();
$event_clubs_term = get_terms( 'uvaclub', array( 'orderby' => 'name', 'order' => 'DESC' ) );
$event_clubs_by_id = array();
foreach( $event_clubs_term as $term ) {
$event_clubs_by_id[$term->term_id] = $term;
}
$event_clubs_by_id_reverse = array_reverse( $event_clubs_by_id );
$parents = array( '0' );
while ( !empty( $parents ) ) {
$parents_copy = $parents;
foreach ( $event_clubs_by_id_reverse as $term ) {
if ( in_array( $term->parent, $parents_copy ) ) {
$parents[] = $term->term_id;
unset( $event_clubs_by_id[$term->term_id] );
$event_clubs_by_id = TribeEvents::array_insert_after_key( $term->parent, $event_clubs_by_id, array( $term->term_id => $term ) );
}
}
$parents = array_diff( $parents, $parents_copy );
}
$child_spacer = '&nbsp;&nbsp;';
foreach( $event_clubs_by_id as $cat ) {
$child_depth = 0;
$parent_id = $cat->parent;
while ( $parent_id != 0 ) {
$child_depth++;
$parent_id = $event_clubs_by_id[$parent_id]->parent;
}
$child_indent = str_repeat($child_spacer, $child_depth);
$event_clubs[] = array(
'name' => $child_indent . $cat->name,
'value' => $cat->term_id,
'data' => array(
'slug' => $cat->slug,
),
);
}
return $event_clubs;
}
protected function setup_query_args() {
$this->queryArgs = array( 'tax_query' => array( array(
'taxonomy' => 'uvaclub',
'field' => 'id',
'terms' => $this->currentValue,
'include_children' => false,
) ) );
}
}
// This adds our new filter to the Filterbar options
// Invokes TribeEventsFilter::__construct($name, $slug);
function CustomClubs_taxfilter(){
new TribeEventsFilter_CustomClubs('UVaClub', 'uvaclub');
}
add_action('tribe_events_filters_create_filters','CustomClubs_taxfilter');
@chrisdigital
Copy link

You saved me a lot of hassle.

Copy link

ghost commented Nov 15, 2017

Thanks for sharing this.

@weismannweb
Copy link

Wow this is great. It was created 4 years ago but worked flawlessly today. Thank you for saving me from programming it myself.

@Zuiev
Copy link

Zuiev commented Apr 15, 2018

Hi! Thank you very much for this code. Could you please tell me where to add this code to make it work?

Thanks!

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