Uncached functions
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.
Use wpcom_vip_attachment_url_to_postid()
as the cached VIP alternative to:
Use wpcom_vip_count_user_posts()
as the cached VIP alternative to:
Use wpcom_vip_get_adjacent_post()
as the cached VIP alternative to:
Use wpcom_vip_wp_oembed_get()
as the cached VIP alternative to:
Use wpcom_vip_url_to_postid()
as the cached VIP alternative to:
Use wpcom_vip_old_slug_redirect()
as the cached VIP alternative to:
Previously uncached WordPress Core functions
One of the performance improvements released in WordPress 6.1 was the addition of caching for some database query functions. In all versions of WordPress less than 6.1, the functions listed below are uncached.
get_children()
: This function is similar toget_posts()
and in fact, callsget_posts()
. The problem with this query is that by default, it will run a no-LIMIT query and setsuppress_filters
to true. This will bypass VIP’s query caching and cause unintended performance consequences if a post has too many children. To avoid this, useWP_Query
instead and follow these guidelines:- Do not set the
post_parent
to0
or a false value. Those settings would request every post on the site. - Set the
posts_per_page
to the absolute minimum requirement, and no higher than 100. The lower a value is set forposts_per_page
, the better performance can be expected.
- Do not set the
get_posts()
: UnlikeWP_Query
, the results ofget_posts()
are not cached via Advanced Post Cache.- Use
WP_Query
instead, or set'suppress_filters' => false
. - When using
WP_Query
instead ofget_posts
, remember to setignore_sticky_posts
andno_found_rows
parameters. Both are hardcoded inside of aget_posts
function with the value oftrue
.
- Use
wp_get_post_terms
():get_the_terms()
is a better option, but both can return aWP_Error
object and thus needs a bit of checking before accessing the results.
wp_get_recent_posts()
: Seeget_posts()
.get_page_by_title()
: Before WordPress 6.1.0, usewpcom_vip_get_page_by_title()
. After WordPress 6.2.0, useWP_Query
instead.
The following function was uncached before WordPress 4.6.0:
get_page_by_path()
: Before WordPress 4.6.0, usewpcom_vip_get_page_by_path()
instead.
Last updated: September 17, 2024