0

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 (like [network-posts blogs=”8" layout=”grid” columns=”3"]), but I’m running into some performance issues/doubts.

Here's the issue:

To my understanding (and maybe I’m wrong), when creating and looping through a new WP_Query, only 1 new query should be executed on the database.

When I use the shortcode (without switching to another blog), 7 new database queries are added.

When I switch to another blog (just 1 switch), the additional queries become 32.

The additional queries are called by update_meta_cache() and WP_Post::get_instance()

and comes in the form of an handful of selects:

SELECT post_id, meta_key, meta_value
FROM wp_11_postmeta
WHERE post_id IN (109)
ORDER BY meta_id ASC

and

SELECT *
FROM wp_11_posts
WHERE ID = 109
LIMIT 1

When switching to another blog, this two queries are duplicated for each post that's being loaded.

Is there a way to reduce the number of database queries?

The plugin is on github and this is the link to a simplified branch that I’m using for debugging purposes: https://github.com/langhenet/display-network-posts/tree/debug Here switch_to_blog() is commented in the php code, but you can activate it by uncommenting line 40 and 64.

EDIT: the plugin has just ~70 lines of code distributed over two files. I didn't paste the code here because I thought it would have been more readable on github.

0

You can disable the caching if you set update_post_meta_cache and update_post_term_cache to false while setting the WP_Query arguments.

When caching is disabled the data will be requested from the database only on demand.

You can check my post in here. If you need more info just let me know.

  • thank you for your answer :-) I've tried to add 'update_post_meta_cache' => false ` 'update_post_term_cache' => false` as WP_Query arguments, but it's adding 7 more database queries. Now I'm going to read your whole post and try to understand what I'm doing wrong! – Enrico Cassinelli Dec 30 '16 at 17:38
  • other 7 may be related to some WordPress cronjobs or so. – prosti Dec 30 '16 at 18:47
  • I've read the your post (which was very interesting!). I also turned off Wp Cron, just to be sure and update_post_meta_cache still adds 7 queries to the database. I'm on a minimal setup (standard 2016 theme and no other plugin)... so I can't really understand why it's happening – Enrico Cassinelli Jan 3 '17 at 18:02
  • Go ahead and share these queries if possible. Some database queries are to load the options table. This table is something like persona grata. – prosti Jan 3 '17 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.