Number of DB queries with a new WP_Query
-
Hi,
I’m building (mostly with a self-teaching purpose) a simple plugin to query posts from another blog on a MS network via a shortcode (something like [network-posts blogs=”8″ layout=”grid” columns=”3″]), but I’m running into some performance issues/doubts.To my understading (and maybe I’m wrong), when creating and looping through a new WP_Query, only 1 new query should be executed on the database.
But when I execute my shortcode (without switching to another blog), I see that that 7 more queries are added (I’m checking then with Query Monitor).
If add a switch_to_blog() to the mix (by just switching to 1 blog, not looping through dozen of them), the additional queries become 32.
Most of the additional queries are tied to these functions:
update_meta_cache()
WP_Post::get_instance()and when I use switch_to_blog(), each of these functions call a SELECT for each post that’s being loaded into the loop.
So my question is if this is the expected behaviour, or is there a way to optimize it?
The plugin is on github and this is the link of a simplified branch that I’m using for debugging purposes:
https://github.com/langhenet/display-network-posts/tree/debugHere switch_to_blog() is commented in the php code, but you can activate it by uncommenting line 40 and 64.
Thanks π
Best
- The topic ‘Number of DB queries with a new WP_Query’ is closed to new replies.