Posted: January 16, 2023 at 8:42 am
Hi there, I’m using EE 4 and have an issue with running a query that checks multiple different meta_key / meta_value pairs on an Event post object. I am trying to query specifically for Tickets, but need to take into account some ACF fields on the related Event. Here is a basic example of what I’m trying to achieve:
However, the SQL that this produces only uses one JOIN on the postmeta table:
This always returns 0 results because the single postmeta JOIN means that the resulting JOIN’d row only contains the postmeta where meta_key = ‘level’, which means that the second AND (meta_key = ‘season’ + meta_value = easter’) will never match. When you manually create a similar query with WP_Query and the meta_query option (note: I’m querying Events here instead of Tickets to removed the complicated relationships):
You get the following SQL with multiple postmeta JOINs:
Because postmeta has been included twice, the query can correctly include both the ‘level’ meta_key + meta_value and the ‘season’ meta_key + meta_value and check them separately. I’ve combed through the docs and even tried to parse the EE SQL creation code but I can’t seem to find a fix for this. I also tried to search the support forums but only found one unanswered post from 2016: https://eventespresso.com/topic/filter-events-with-multiple-custom-meta-keys-and-value/ Does anyone know if this possible to do within the current EE functionality, or will I need to come up with a workaround? Thanks in advance! |
|
Sorry for the messy formatting of the code above – here are some screenshots if that’s easier to read:
|
|
Corrected images!
Apologies if I’m being blind and missing the preview / edit buttons for posts somewhere! |
|
Hi there, Firstly thank you for the detailed report, it really does help narrow down to see the issue at hand. Having said that I’d recommend opening up an issue on the repo for this HERE. Our developers do not monitor the support forums and they are the best people to answer this for you. I would basically just be relaying the info to and from the support forums for this so you may as well skip that for the above. |
|
Thanks Tony! Apologies for the late reply – we got to work on a potential alternative in the meantime so we were heads down on that. Unfortunately, the alternative was to try to use a new custom category / taxonomy instead, but that had the same issue with cancelling itself out when also querying on the EE category. I’ll write up an issue on the repo now. Thanks again! |
|
For any future readers that might be affected by this issue, the Github issue is here: https://github.com/eventespresso/event-espresso-core/issues/4121 |
|
The support post ‘Problem querying on multiple meta_keys attached to an Event’ is closed to new replies.
Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.