Filter Bar -> custom taxonomy almost works

Home Forums Calendar Products Filter Bar Filter Bar -> custom taxonomy almost works

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #937242
    Chris Fleming
    Participant

    I used a code snippet that was posted earlier and put that in my functions file after I created a custom taxonomy for my events. The taxonomy is called “division” and shows up on the event and the filter bar. On it’s own it filters the events and works fine. If I select a “division” and venue it filters properly too. However if I select a category option and “division” option (custom taxonomy) filtering no longer works properly and it just combines the results. If I have 2 events, 1 is in the men’s division and 1 is in the females division and both are under the category “professional”, I should be able to select “professional” from the category and “men’s” from the custom taxonomy to display one result. Unfortunately this displays both events :(. If I uncheck the professional category it will just display the men’s custom option I have selected. So on it’s own it works. Here is the code I’m using. I feel like there should be fairly simple solution. Thanks for the help.

    /**
    * Quick example of adding a custom filter
    * A simple copy and paste of the existing Category filter
    * Refer to the last line for how it adds itself to the interface
    * Find this new filter in: WP Admin > Events > Settings > Filterbar
    * Docs for TribeEventsFilter: http://docs.tri.be/Filter-Bar/class-TribeEventsFilter.html
    */
    class TribeEventsFilter_Custom 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_categories = array();
    $event_categories_term = get_terms( ‘division’, array( ‘orderby’ => ‘name’, ‘order’ => ‘DESC’ ) );
    $event_categories_by_id = array();
    foreach( $event_categories_term as $term ) {
    $event_categories_by_id[$term->term_id] = $term;
    }
    $event_categories_by_id_reverse = array_reverse( $event_categories_by_id );
    $parents = array( ‘0’ );
    while ( !empty( $parents ) ) {
    $parents_copy = $parents;
    foreach ( $event_categories_by_id_reverse as $term ) {
    if ( in_array( $term->parent, $parents_copy ) ) {
    $parents[] = $term->term_id;
    unset( $event_categories_by_id[$term->term_id] );
    $event_categories_by_id = TribeEvents::array_insert_after_key( $term->parent, $event_categories_by_id, array( $term->term_id => $term ) );
    }
    }
    $parents = array_diff( $parents, $parents_copy );
    }
    $child_spacer = ‘  ‘;
    foreach( $event_categories_by_id as $cat ) {
    $child_depth = 0;
    $parent_id = $cat->parent;
    while ( $parent_id != 0 ) {
    $child_depth++;
    $parent_id = $event_categories_by_id[$parent_id]->parent;
    }
    $child_indent = str_repeat($child_spacer, $child_depth);
    $event_categories[] = array(
    ‘name’ => $child_indent . $cat->name,
    ‘value’ => $cat->term_id,
    ‘data’ => array(
    ‘slug’ => $cat->slug,
    ),
    );
    }
    return $event_categories;
    }

    protected function setup_query_args() {
    $this->queryArgs = array( ‘tax_query’ => array( array(
    ‘taxonomy’ => ‘division’,
    ‘field’ => ‘id’,
    ‘terms’ => $this->currentValue,
    ‘include_children’ => false,
    ) ) );
    }

    }

    // This adds our new filter to the Filterbar options
    // Invokes TribeEventsFilter::__construct($name, $slug);
    $taxonomy_example = new TribeEventsFilter_Custom(‘Taxonomy’, ‘taxonomy_filter’);

    #937252
    Chris Fleming
    Participant

    I was able to fix that but if I add 2 custom taxonomies for filters now they conflict with one another :/

    /**
     * Add custom FILTER ("division") - Men's / Women's / Coed / Rev-Coed
     */
    
    /**
     * 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 = 'dropdown';
     
      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( 'division', 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' => 'division',
          '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('Division', 'division');
    }
    add_action('tribe_events_filters_create_filters','CustomClubs_taxfilter');
    
    /**
     * Add custom FILTER ("format") - 2's / 4's / 6's
     */
    
    /**
     * 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_format 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_format = array();
        $event_format_term = get_terms( 'format', array( 'orderby' => 'name', 'order' => 'DESC' ) );
        $event_format_by_id = array();
        foreach( $event_format_term as $term ) {
          $event_format_by_id[$term->term_id] = $term;
        }
        $event_format_by_id_reverse = array_reverse( $event_format_by_id );
        $parents = array( '0' );
        while ( !empty( $parents ) ) {
          $parents_copy = $parents;
          foreach ( $event_format_by_id_reverse as $term ) {
            if ( in_array( $term->parent, $parents_copy ) ) {
              $parents[] = $term->term_id;
              unset( $event_format_by_id[$term->term_id] );
              $event_format_by_id = TribeEvents::array_insert_after_key( $term->parent, $event_format_by_id, array( $term->term_id => $term ) );
            }
          }
          $parents = array_diff( $parents, $parents_copy );
        }
        $child_spacer = '&nbsp;&nbsp;';
        foreach( $event_format_by_id as $cat ) {
          $child_depth = 0;
          $parent_id = $cat->parent;
          while ( $parent_id != 0 ) {
            $child_depth++;
            $parent_id = $event_format_by_id[$parent_id]->parent;
          }
          $child_indent = str_repeat($child_spacer, $child_depth);
          $event_format[] = array(
            'name' => $child_indent . $cat->name,
            'value' => $cat->term_id,
            'data' => array(
              'slug' => $cat->slug,
            ),
          );
        }
        return $event_format;
      }
     
      protected function setup_query_args() {
        $this->queryArgs = array( 'tax_query' => array( array(
          'taxonomy' => 'format',
          'field' => 'id',
          'terms' => $this->currentValue,
          'include_children' => false,
        ) ) );
      }
     
    }
    // This adds our new filter to the Filterbar options
    // Invokes TribeEventsFilter::__construct($name, $slug);
    function Format_taxfilter(){
      new TribeEventsFilter_format('Format', 'format');
    }
    add_action('tribe_events_filters_create_filters','Format_taxfilter');
    #937682
    Barry
    Member

    Hi there Chris,

    Looks like you’ve made a great start 🙂

    Unfortunately, right now, trying to create custom taxonomy filters in this way is going to be prone to causing conflicts (with other taxonomy-based filters), hence why you find you cannot use both simultaneously and achieve the results you expect.

    What I’d recommend here is removing your existing setup_query_args() methods. Instead, set up a tribe_events_pre_get_posts filter – essentially no different to the generic WordPress pre_get_posts filter hook, except that it is only fired for event-related queries – and apply your custom taxonomy queries there.

    Otherwise, regrettably, your custom filters and the existing category filter will simply override one another’s taxonomy queries.

    Out of interest, did you base your work on an existing knowledgebase article (can you link me to it if so)?

    Thanks!

    #938267
    Chris Fleming
    Participant

    Thanks I will give this a shot and let you know what happens. I found a support question and used some info from that but I can’t seem to find it now :/ Originally it ended with this:

    new TribeEventsFilter_Format('Format', 'format');

    This worked as a filter on its own but not with the original event filter so I had to change it to this:

    function Format_taxfilter(){
      new TribeEventsFilter_format('Format', 'format');
    }
    add_action('tribe_events_filters_create_filters','Format_taxfilter');

    This allowed one filter to work properly and play nice with the other filters. But I guess there was only room for 1 new friend at that party.

    #938290
    Chris Fleming
    Participant

    Where should I run this? In the class doesn’t seem to change the query?

    	function my_search_filter() {
    		$taxquery = array(
    			array(
    			  'taxonomy' => 'format',
    			  'field' => 'id',
    			  'terms' => $this->currentValue,
    			  'include_children' => false,
    			)
    		);
    		$query->set( 'tax_query', $taxquery );
    		
    		return $query;
    		}
    		
    	add_filter( 'tribe_events_pre_get_posts', $query );
    #938291
    Barry
    Member

    OK – I just wanted to ensure it wasn’t an official knowledgebase article that highlighted this strategy but had escaped our attention (if it was then we’d of course have wished to amend it) 🙂

    Certainly for the time being, multiple custom filters that operate on custom taxonomies would need a different approach like the one I outlined to play nicely with one another and with the plugin’s own event category filter.

    #938887
    Chris Fleming
    Participant

    Barry, seems the the last piece to the puzzle is adding “tribe_events_pre_get_posts” filter correctly. I set this up as suggested and posted the code. Should I be adding this to the extended class, I haven’t had luck with that. Help getting this last piece setup would be great. Seems this would also put an end to all the questions everyone is asking on the same issue.

    Side Note: When are the additional fields going to be added to the filter bar add-on? I see this was mentioned almost a year ago but there hasn’t been any news. The same goes for adding documentation for the custom taxonomy filter development. At least if the last piece to the puzzle (query output) is resolved you would have working code to give to people.

    #938920
    Barry
    Member

    Apologies first of all Chris – I posted my last reply only a few minutes after your own follow up question (on Jan 27) and as I already had the editor open I didn’t actually see what you had written before replying.

    Should I be adding this to the extended class, I haven’t had luck with that.

    I would recommend doing so, simply because you can then easily access object properties and so on in order to set the taxonomy query up.

    The basic approach you outlined though is essentially going to result in the very same problem we want to avoid: that is, you’re setting the tax_query without first checking to see if one already exists/what terms might already be being queried.

    Help getting this last piece setup would be great. Seems this would also put an end to all the questions everyone is asking on the same issue.

    I’d love to help more but we’re very much in customization territory here – and so it’s really something that you will need to drive forward under your own steam 🙂

    In short, though:

    • Setting the tax_query without checking to see what might already be set up is not going to work if you want this to play nicely with other taxonomy-based filters
    • You need to use $query->get( ‘tax_query’ ), inspect the result and merge it with your new query

    I’m sorry I can’t do more to help with this one, but I hope that at least gives you a steer in the right direction.

    When are the additional fields going to be added to the filter bar add-on? I see this was mentioned almost a year ago but there hasn’t been any news. The same goes for adding documentation for the custom taxonomy filter development.

    Right at this moment we can’t offer an ETA for custom/additional field support in Filter Bar and realistically I’d suggest it will not be available in the space of the next couple of releases – though it’s always possible we might be able to squeeze it in.

    In terms of documentation for custom taxonomy filter development, I’m not actually aware of plans for such a specific resource – but we are always aiming to improve our documentation and you’re more than welcome to post feature requests for things like this.

    We do have an issue logged to consider revising the way taxonomy filtering is handled, though, so any very specific learning resources about this aspect of development would probably wait upon completion of further work in that area.

    I hope that helps and I wish you luck with the customization – as I don’t think we’re going to be able to help further on this specific item though I’ll go ahead and close out the topic but please don’t hesitate to create new topics as needed if any other issues crop up 🙂

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘Filter Bar -> custom taxonomy almost works’ is closed to new replies.