6

My theme calls the update_meta_cache() function 58 times per page! This function appears to run the following query based on the post in question:

SELECT post_id, meta_key, meta_value
-> FROM wp_postmeta
-> WHERE post_id IN (81649)
-> ORDER BY meta_id ASC
-> ;

The vast majority of the meta_keys (and corresponding values) returned are not necessary for the page in question (e.g. yoast_wpseo_title, _edit_last, _edit_lock are not needed for the homepage loop).

Is there a way to reduce or prevent calling of the update_meta_cache? Perhaps a way to include in a function on the pre_get_posts hook?

1

2 Answers 2

2

Whenever you use the function get_post_meta(), the above query will be performed to get all post meta and store in the cache. The more posts you query, the more queries are made.

To reduce the number of queries, we should pre-cache all meta values of all posts in the query before calling get_post_meta.

This is the sample code taken from a tutorial:

add_filter( 'posts_results', 'cache_meta_data', 9999, 2 );
function cache_meta_data( $posts, $object ) {
    $posts_to_cache = array();
    // this usually makes only sense when we have a bunch of posts
    if ( empty( $posts ) || is_wp_error( $posts ) || is_single() || is_page() || count( $posts ) < 3 )
        return $posts;

    foreach( $posts as $post ) {
        if ( isset( $post->ID ) && isset( $post->post_type ) ) {
            $posts_to_cache[$post->ID] = 1;
        }
    }

    if ( empty( $posts_to_cache ) )
        return $posts;

    update_meta_cache( 'post', array_keys( $posts_to_cache ) );
    unset( $posts_to_cache );

    return $posts;
}
5
  • 2
    I just came across this issue, as I'm seeing a ton of update_meta_cache() requests being made. With this tweak in my themes function.php file, I'm still seeing lots of these kind of queries? SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (8987) ORDER BY meta_id ASC ... am I doing something wrong? Mar 3, 2016 at 8:51
  • 1
    No effect whatsoever, it's bad idea to use random 2011 code on latest wp so much changed. Mar 5, 2018 at 16:48
  • I simply searched "update_meta_cache" in "whole world press folder" and terminated.. Now website is running like i'm using offline.. super fast.
    – MFarooqi
    Feb 13, 2020 at 19:51
  • DON'T CACHE ALL (SITE) POSTS AT ONCE - what author means here are all posts fetched for a particular page, don't attempt to pre-cache hundreds, thousands, mlions etc. of posts, it could kill your site, if you need extensive caching, use per-page caching solution or plugin. (the bold text just seems misleading for random-newcomers)
    – jave.web
    Feb 18, 2021 at 9:17
  • 1
    Hi @MFarooqi could you elaborate a little bit further how you fixed that? I'm still having the same issue here.
    – Leo
    Mar 4, 2022 at 20:46
1

For a WP_Query, caching can be disabled by setting certain caching parameters to false.

Similarly for WP_Term_Query, it can be disabled by including update_term_meta_cache => false in the query args.

Additionally, the filter update_{$meta_type}_metadata_cache can be used to disable metadata caching for all queries on the object type. (ex. update_post_metadata_cache, update_term_metadata_cache, etc)

1
  • Thank you for your suggestions, @plong0, this seems to be exactly the thing I've been looking for. Fun experiments with less meta caching await me :)
    – vlood
    Jun 27, 2023 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.