For various reasons, some functions in WordPress core are purposely uncached. Because of this, calling those uncached functions in application code will always result in an SQL query. This can have performance implications for a site, particularly if an uncached function is called by code during a high traffic event. A large volume of direct SQL queries can overload the primary database and lead to an increase in responses with a
503 HTTP status code.
Cached VIP alternative functions
To prevent issues caused by uncached functions, VIP MU plugins provides several alternative versions of functions that will be cached when called on the VIP Platform.
wpcom_vip_attachment_url_to_postid() as the cached VIP alternative to:
wpcom_vip_count_user_posts() as the cached VIP alternative to:
wpcom_vip_get_adjacent_post() as the cached VIP alternative to:
wpcom_vip_wp_oembed_get() as the cached VIP alternative to:
Previously uncached WordPress core functions
One of the performance improvements released in WordPress v6.1 was the addition of caching for some database query functions. In all versions of WordPress less than v6.1, the functions listed below are uncached.
get_children(): This function is similar to
get_posts()and in fact, calls
get_posts(). The problem with this query is that by default, it will run a no-LIMIT query and set
suppress_filtersto true. This will bypass VIP’s query caching and cause unintended performance consequences if a post has too many children. To avoid this, use
WP_Queryinstead and follow these guidelines:
- Do not set the
0or a false value. Those settings would request every post on the site.
- Set the
posts_per_pageto the absolute minimum requirement, and no higher than 100. The lower a value is set for
posts_per_page, the better performance can be expected.
- Do not set the
WP_Query, the results of
get_posts()are not cached via Advanced Post Cache.
WP_Queryinstead, or set
'suppress_filters' => false.
- When using
get_posts, remember to set
no_found_rowsparameters. Both are hardcoded inside of a
get_postsfunction with the value of
get_the_terms()is a better option, but both can return a
WP_Errorobject and thus needs a bit of checking before accessing the results.
Last updated: November 22, 2022