max-kk
3/22/2018 - 1:40 PM

How to add custom filter field to Tribe Events Calendar without Filter addon (in this example - filter by category and tag). Example: https:

How to add custom filter field to Tribe Events Calendar without Filter addon (in this example - filter by category and tag). Example: https://monosnap.com/file/kD9Liz759zrsxi6lnSL49KHxAu8haX

<?php

// Example: https://monosnap.com/file/kD9Liz759zrsxi6lnSL49KHxAu8haX
// Tested with Tribe Events Calendar 4.6.12

/**
 * Set up the keyword search in the tribe events bar.
 *
 * @param array $filters The current filters in the bar array.
 *
 * @return array The modified filters array.
 */
add_filter( 'tribe-events-bar-filters', function($filters){
    $selected_category = '';
    if ( ! empty( $_REQUEST['tribe-category-search'] ) ) {
        $category = sanitize_title ($_REQUEST['tribe-category-search'] );
    } 
    
    $selected_tag = '';
    if ( ! empty( $_REQUEST['tribe-tag-search'] ) ) {
        $selected_tag = sanitize_title ($_REQUEST['tribe-tag-search'] );
    }

    $event_categories = calendar_get_categories_list();
    $event_tags = calendar_get_tags_list();

    $event_categories_html = sprintf('<option value="">%s</option>', 'Select Category');
    foreach ($event_categories as $event_tax) {
        $event_categories_html .= sprintf('<option value="%2$s" %3$s>%1$s</option>',
            $event_tax->name, $event_tax->slug, selected($event_tax->slug,$selected_category, false ));
    }
    
    $event_tags_html = sprintf('<option value="">%s</option>', 'Select Tag');
    foreach ($event_tags as $event_tax) {
        $event_tags_html .= sprintf('<option value="%2$s" %3$s>%1$s</option>',
            $event_tax->name, $event_tax->slug, selected($event_tax->slug,$selected_tag, false ));
    }

    $filters['tribe-tag-search'] = array(
        'name'    => 'tribe-tag-search',
        'caption' => esc_html__( 'Category', 'the-events-calendar' ),
        'html'    => '<select type="text" name="tribe-tag-search" id="tribe-tag-search">' . $locations_options_html . '</select>',
    );

    $filters['tribe-category-search'] = array(
        'name'    => 'tribe-category-search',
        'caption' => esc_html__( 'Tags', 'the-events-calendar' ),
        'html'    => '<select type="text" name="tribe-category-search" id="tribe-category-search">' . $event_tags_html . '</select>',
    );

    return $filters_new;
}, 11, 1 );


function calendar_get_tags_list() {

    $r = wp_cache_get("calendar_get_tags_list");

    if ( $r ) {
        return $r;
    }


    $all_tags = get_terms( array('taxonomy' => 'post_tag', 'hide_empty'=>true) );

    $tags_by_SLUG = array();

    foreach ($all_tags as $terms_ID => $term) {
        $tags_by_SLUG[$term->slug] = $term->name;
    }

    wp_cache_add("calendar_get_tags_list", $tags_by_SLUG);

    return $tags_by_SLUG;
}

function calendar_get_categories_list() {

    $r = wp_cache_get("calendar_get_categories_list");

    if ( $r ) {
        return $r;
    }


    $all_tags = get_terms( array('taxonomy' => Tribe__Events__Main::TAXONOMY, 'hide_empty'=>true) );

    $tags_by_SLUG = array();

    foreach ($all_tags as $terms_ID => $term) {
        $tags_by_SLUG[$term->slug] = $term->name;
    }

    wp_cache_add("calendar_get_categories_list", $tags_by_SLUG);

    return $tags_by_SLUG;
}

/**
 * Filter posts by our Category or Tag slug
*/
add_filter('pre_get_posts', function($query) {
    
    /** @var WP_Query $query */

    if ( !empty( $_REQUEST['tribe-category-search'] ) ) {
        $category_slug = sanitize_title( $_REQUEST['tribe-category-search'] );

        $tax_query[] = array(
            'taxonomy'         => Tribe__Events__Main::TAXONOMY,
            'field'            => 'slug',
            'terms'            => [$category_slug],
            'include_children' => false,
            'operator'         => 'IN',
        );

        $query->set( 'tax_query', $tax_query );
    }

    if ( !empty( $_REQUEST['tribe-tag-search'] ) ) {
        $location_slug = sanitize_title( $_REQUEST['tribe-tag-search'] );

        $tax_query[] = array(
            'taxonomy'         => 'post_tag',
            'field'            => 'slug',
            'terms'            => [$location_slug],
            'include_children' => false,
            'operator'         => 'IN',
        );

        $query->set( 'tax_query', $tax_query );
    }


    return $query;
}, 55 );