WordPress core has a number of functions that, for various reasons, are uncached, which means that calling them will always result in an SQL query. Listed below are some of these uncached functions and their VIP cached alternatives.
get_adjacent_post(), get_previous_post(), get_next_post(), previous_post_link(), next_post_link()
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_filters to true. That can cause problems because it will skip our query caching (brought to you by Advanced Post Cache), and cause unintended performance consequences when a post has too many children. We’ve seen instances where a simple
get_children() call for an attachment can cause 503’s on a site due to the amount of database calls and cache updates it can generate. We recommend that you use
WP_Query instead 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 your absolute minimum requirement, and no higher than 100. The lower the value you set for
posts_per_page, the better performance you can expect.
WP_Query, the results of
get_posts() are not cached via Advanced Post Cache.
WP_Query instead, or set
'suppress_filters' => false.
$args = array( 'post_type' => 'post', 'posts_per_page' => 3, 'suppress_filters' => false, ); $query = get_posts( $args );
WP_Query instead of
get_posts don’t forget about setting
no_found_rows params appropriately (both are hardcoded inside a
get_posts function with value of
get_the_terms is a better option, but both can return a
WP_Error object and thus needs a bit of checking before accessing the results.