How to cache/speed WordPress queries for logged in users?

Moreover as you can see all the queries are pretty fast

Those queries aren’t so fast. 0.1s for a simple lookup on the options table is very slow.

All of the queries in the image you posted are on the options table. I’m not sure if most of your queries are option queries or not, but if you want to reduce the total number of option queries, you can use the autoload parameter on update_option or add_option. From the documentation:

$autoload
(string) (optional) Should this option be automatically loaded by the function wp_load_alloptions() (puts options into object cache on each page load)? Valid values: yes or no.

If you autoload your options they will all be fetched in a single query. This will reduce the total number of queries, but as a mentioned before, your queries shouldn’t be taking that long to begin with.

For existing options, you will have to delete them first and then re-add them using the autoload parameter:

$val = get_option('some_option');
delete_option('some_option');
update_option('some_option', $val, true);

By default, when you create options they are set to autoload so it is a bit odd that you have so many options that aren’t autoloading.

Leave a Comment