Support

Account

Home Forums Backend Issues (wp-admin) Nested meta_query causing database to time out (or give HTTP error 502) Reply To: Nested meta_query causing database to time out (or give HTTP error 502)

  • This isn’t an inefficiency or ACF. It’s more of a problem with WP, although, that’s not really the case either. The problems are that your query is creating many, many joins doing multiple searches on a database column than is not optimized for searching.

    This is explained in many places like https://wordpress.stackexchange.com/questions/158898/meta-query-terribly-slow and https://tommcfarlin.com/wp_query-and-multiple-meta-keys/.

    There might not be a way to actually correct this, what follows is only a possible work around that has not been tested.

    1. Split your OR meta queries and do 3 separate queries
    2. Return just the post IDs from these 3 queries
    3. Use array_merge() http://php.net/manual/en/function.array-merge.php to combine the results of the previous queries and array_unique() http://php.net/manual/en/function.array-unique.php to get only unique values $ids = array_unique(array_merge($array1, $array2, $array3));
    4. Use the resulting array to do a 4th query using this array as the post__in argument. You would only need to do your sorting by the date in this last query.