0

I have a Buddypress membership page, and for specific customizations I need a few usermeta fields being loaded all the time, for the currently logged user as well as (in case any) the displayed user-id.

Hence, I'm wondering if there is such a mechanism to "auto load" specific usermeta fields all at once during pageload to reduce the number of single queries done via get_user_meta() in case triggered/programmed manually?

1 Answer 1

2

WordPress already does this

When you fetch a user post or term WP will auto-fetch all of its meta and store it in WP Cache. This way if you need to use any of that post/term/users meta it's already there. The thinking is to prefetch in bulk in a single query since it's very rare that the meta isn't used.

This way the data is fetched only once per request, but if you have a persistent object cache it can survive across requests leading to dramatic performance gains. In a best case scenario with an object cache and a well built site there may be no database queries at all.

If you look at the database queries with a tool such as Query Monitor you'll see that additional calls to get_post_meta do not generate database queries.

The Exception

There is an exception though! If you decide to start fetching just post IDs to try and be efficient with your database calls, WordPress won't pre-fetch these meta values, and it'll generate a small database query when you try to use that raw user/post ID. As a result the optimisation of only fetching post IDs actually slow the site down!

Manually Priming Cache

In those cases such as above, you can fetch all post meta at once by using get_post_meta( $post_id ); or get_user_meta( $user_id ); and it'll be returned as an array and cached. Likewise get_post and the user equivalent will prime caches, including terms.

But note that if you are using WP_Query or WP_User_Query normally, this is unnecessary, and in some circumstances may have a performance hit due to extra memory consumption or unnecessary checking.

Measure!

As with most performance, measure things and double check hard reality first. Unless you see firsthand lots of tiny queries, don't try to optimise them away, especially if they're not there to begin with.

A common beginner mistake is to implement something like WP Cache because they're unaware of it, leading to an increased memory footprint and caching bugs with no measurable performance gains. Those that do see performance gains only see it as a reduction of the performance hit they received when they started requesting posts and users only have specific fields returned. Don't request bespoke couture one of a kind results, request factory line standardised cachable results that can be churned out by the millions for every possible use case.

2
  • Thanks, I will play around with Query Monitor to look exactly after the Queries added (or not in optimal case) when programming different type of usermeta queries.
    – tim
    Commented Jun 28, 2023 at 12:06
  • note that even if it didn't do this, get_user_meta/get_post_meta/etc are some of the fastest and lightest DB queries you can do in WordPress, using meta_query on the other hand...
    – Tom J Nowell
    Commented Jun 28, 2023 at 13:29

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.