Meta_query needs relation arg per key/value pair
|Reported by:||CoenJacobs||Owned by:|
Since #17011 introduced the relation param to meta_query args, it is possible to set it to 'AND' or 'OR' to define how the multiple key/value pairs should be related to each other. This can only be set for the entire meta_query array, so it will be applied to all key/value pairs.
Imagine the following meta_query array, where I want to get all posts where '_kind' is 'value_1' or 'value_2'. Either way, '_exclude' should ALWAYS be '-1'. This array will not work, since if any of the key/value pairs match, that post will be returned:
[meta_query] => Array (  => Array ( [key] => _kind [value] => value_1 )  => Array ( [key] => _kind [value] => value_2 )  => Array ( [key] => _exclude [value] => -1 ) [relation] => OR )
So, I had to think of a way to set the relation per key/value pair. This would involve adding an extra array inside the meta_query array. Each group of key/value pairs can have its own 'relation' param, making the meta_query able to properly structure the query. Meta_query array will look something like this:
[meta_query] => Array (  => Array ( [relation] => OR  => Array ( [key] => _kind [value] => value_1 )  => Array ( [key] => _kind [value] => value_2 ) )  => Array ( [relation] => AND  => Array ( [key] => _exclude [value] => -1 ) ) [relation] => AND )
Haven't figured out what would need to be changed to the WP_Query and WP_Meta_Query classes to support this change, but that shouldn't involve anything more than an extra loop (easy to port for backwards compat). Only thing I'm worried is what this will do to performance.
Change History (5)
- Keywords close added
comment:4 @CoenJacobs — 3 years ago
- Keywords dev-feedback close removed
- Resolution set to wontfix
- Status changed from new to closed