W3 Total Cache Optimization: Caching Post Taxonomies

Today I installed Query Monitor and noticed that each post on my homepage was triggering 2 database queries. One query retrieved the post_tag post taxonomy and one retrieved the post_format post taxonomy. Since my homepage has up to 15 posts, there can be 30 extra queries. These values should have been fetched all at once; they should not be fetched from the database individually.
Database queries logged by Query Monitor
I disabled my plugins one-by-one and realized that the issue was caused by W3 Total Cache. Here’s how it looks like after I disabled W3 Total Cache:
Queries with W3 Total Cache disabled
There are no more queries fetching post tags or post formats for individual posts. They are batched into one query in wp_get_object_terms() (which is called by _prime_post_caches()). WordPress caches the results of the query in a WP_Object_Cache object.
After digging through W3 Total Cache’s code, I realized that posts were being cached to disk while post taxonomies weren’t. When _prime_post_caches() runs, it checks if there are any posts that aren’t in the cache. If there aren’t any posts, then it doesn’t do anything. WordPress assumes to if a post is cached, then all associated post taxonomies are cached. However, W3 Total Cache didn’t cache the post taxonomies.
My W3 Total Cache settings seemed to be correct. I tried finding a way to get W3 Total Cache to cache post taxonomies, but there was too much code to go through. Instead, I opted for a somewhat hacky solution.

If the post taxonomies aren’t cached, the snippet primes (prepopulates) the taxonomy cache by calling update_object_term_cache(). Since W3 Total Cache also prevents the post meta cache from being primed, I prime that as well. However, W3 Total Cache caches post meta, so it isn’t necessary to prime the post meta.
After adding the snippet, my homepage went from ~30 queries to 6 queries.
After inserting snippet

Checking if you have this issue

To see if you have this issue, you can download Query Monitor. You can also put something like the following in your functions.php: