Adding a custom filter

Home Forums Calendar Products Filter Bar Adding a custom filter

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #743472
    jorditost
    Participant

    Hi,

    Like other people in this forum, I am trying to figure out, how to add custom filters based on custom taxonomies. I found this other threads:

    https://tri.be/support/forums/topic/when-will-you-release-the-ability-to-configure-filter-bar-with-custom-categories/
    https://theeventscalendar.com/support/forums/topic/adding-custom-filter-to-filter-bar/
    https://theeventscalendar.com/support/forums/topic/how-can-i-change-the-venue-filter-to-show-the-city-or-state-field/

    But in none of them there is the right solution or an example of doing it.

    I created a new filter class (based on the TribeEventsFilter_Category class) named TribeEventsFilter_City. Since here is everything ok. But, how can I attach it to the filters array? As Brook answers here, <i>”Once you have created your TEC filter you would need to attach it. How I would do it is by adding my TEC filter to the tribe_events_all_filters_array. That is WP filter that you can hook into with add_filter. Simply add your TEC filter to the end of that array and return.”</i>

    As Barry points in this thread, this must be like this:

    add_filter( 'tribe_events_all_filters_array', 'custom_add_filter_callback' );
    function custom_add_filter_callback( $filters ) {
    	$filters['unique_slug'] = array(
    		'name' => 'your_filter_name',
    		'type' => '''',
    		'admin_form' => '',
    	);
    	return $filters;
    }
    

    But, what must be this “unique_slug”? And “your_filter_name”? “type”? How can be this item “related” to my new class? Or where must be the new instance of the class created? I mean something like this (like in the initialize_filters() function in tribe-filter-view.class.php).

    Could you post an example with a class and a tribe_events_all_filters_array callback?

    Thanks for your support!

    #743483
    jorditost
    Participant

    I got the solution. I post here the code since it seems other people had the same question and there was no clear answer.

    Inside my theme’s folder I created a new php file for my new filter inside the filters folder. In my case on:

    filters/TribeEventsFilter_LocationCity.php
    

    You can base the behaviour of your new filter on the predefined filters in the plugin (for example, libs/filters/TribeEventsFilter_Category.php)

    And then, I just added a new intance of the new class in my functions.php file this way:

    new TribeEventsFilter_LocationCity( __( 'City', 'tribe-events-filter-view' ), 'city' );
    

    (as it is done in the initialize_filters() function in lib/tribe-filter-view.class.php)

    I didn’t have to use the tribe_events_all_filters_array filter at all as it was pointed in other threads. Now I can add my new filter in the Filter tab of the plugin’s settings page.

    Please correct me if if there is a better practice 😉

    #745000
    Brian
    Keymaster

    jorditost, glad you were able to find a solution and thanks for sharing your solution.

    That is the correct way to do things.

    Since it look liked you figured this out I am going to close this ticket, if you have any more questions related to this just reference this ticket in a new one.

    Thanks

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Adding a custom filter’ is closed to new replies.