multiple custom filters?
Please note that when it comes to custom coding efforts the degree and depth of support we can offer tends to be limited. With that said, I’d be happy to run over a few points that will hopefully be helpful 🙂
I have several custom filters, each one has a class that extends TribeEventsFilter
That will still work, but I’d recommend you update your code to extend Tribe__Events__Filterbar__Filter instead (assuming your copies of our plugins are up-to-date, that is).
However, I’ve noticed that when multiple filters are applied, only one seems to be effective at a time. Is TribeEventsFilter->setup_query_args such that it just overwrites the query args from other filters? Do I need to concatenate the args in some way?
It’s up to you / it’s the responsibility of each filter class to do things in a way that will play well with other filters.
If some of your filters are going to modify the same query properties, then concatenating or finding another logical way to merge these as needed is indeed going to be the best way ahead.
I’ve switched to Tribe__Events__Filterbar__Filter. However, I’m still struggling with getting multiple filters applied. It’s odd as I’m sure this worked all those years ago when the site was first developed. I’ve just had to re-do a lot of the events stuff to work with a recent update, maybe something got lost in the process. I was hoping that things would be gained in the process, but it seems maybe I’ve solved some problems only to encounter others.
As far as I can see each custom filter is setting up its own query args fine through setup_query_args(). However each filter is adding its args to the WP_Query via addQueryArgs() in the parent class Tribe__Events__Filterbar__Filter. I guess $query->set( $key, $value ); is simply overwriting each filter’s meta_query with the next filter’s value. So at first glance it looks like, the way the parent class is engineered, there’s simply no way to have multiple filters applied at once? I’ve not worked it out yet but it seems like some complex additional coding around the pre_get_posts hook, outside but interacting with the EC filters system, would be necessary. Could you confirm this before I spend time heading down that route?
We wouldn’t intentionally make something like this difficult and, in fact, Filter Bar supports exactly this scenario: you can concurrently apply category, tag, cost and various other filters.
I guess $query->set( $key, $value ); is simply overwriting each filter’s meta_query with the next filter’s value.
So is the specific problem you are facing one with the meta query, then? Our Additional Fields filter (which will function if you also have Events Calendar PRO active) does indeed operate on that however it attempts to preserve any existing meta_query fields that have been set, so far as is practicable at least.
The level of support we can provide for custom code is limited, but if you share what you have (by uploading a zip or by posting your code on Pastebin, Gist or similar) I’ll happily take a look in case there’s something we’ve overlooked 🙂
Yes, the problem is the meta_query, because in the parent class method Tribe__Events__Filterbar__Filter->addQueryArgs(), which I assume runs for each custom filter child class, the queryArgs are added very simply, so a meta_query set up by one filter will be overwritten by the next filter called. I thought I just needed to concatenate the meta_query arrays in setup_query_args() in the custom filter child class, but this is self-contained. Concatenating here has no effect, since whatever set is simply overwritten by the core Tribe__Events__Filterbar__Filter class.
I can’t find any details about coding this here: https://theeventscalendar.com/knowledgebase/creating-custom-filters-for-filter-bar/ And I’ve looked at the Pro ‘Additional Fields’ (https://theeventscalendar.com/knowledgebase/pro-additional-fields/) and it makes me wonder if you’re misunderstanding. That’s about adding custom fields. We have our own custom fields system. The issue is how the values of these fields are queried when the filters they’re connected to are applied.
I appreciate that in some respects you can’t help with customisation, but you provide the ability to add custom filters by extending Tribe__Events__Filterbar__Filter, and it’s not at all clear how you allow multiple filters to be applied at once – which would seem to be basic functionality. As far as I can see now, this basic functionality is prevented by how the core filter class is structured. Again, I’m probably missing something, please point it out if so.
Sure – so let’s ensure we’re both talking about the same thing and are working with the same code.
Can you share (privately if you prefer) an example of one or more of the custom filters you wrote that is having problems interacting with other filters — and provide any notes that will help me to quickly replicate locally?
Hey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.
Thanks so much!
The Events Calendar Support Team
The topic ‘multiple custom filters?’ is closed to new replies.