WP_Query with many meta_query arguments taking a long time to load

Yeah I can imagine that query is going to be a doozy.

The main reason for that is that every extra condition against a post meta value might be causing an extra SQL JOIN between the wp_posts table and the wp_posts table, and JOINS can get exponentially expensive. I’m surprised it even comes back in a minute if you select all those options. And this problem will get worse as the size of your wp_postmeta table grows.

Saying that, it’s not clear why you’re using LIKE here instead of = for the compare value, unless you really do need to match strings that contain the value parameter? That might make a big difference in the speed as LIKE is more expensive than = in SQL.

Other than that, because of the way this query works internally in WP_Query you might have a hard time making it go any faster. You might be at the point where you need a different data structure, or to do something more custom away from using WP_Query and the key-value structure of the wp_postmeta table. Or you could look at limiting the number of these options that users can pick.

HTH

Leave a Comment