Tips & Tricks for Optimizing ACF Performance in WordPress
There was recently a discussion on The Admin Bar Facebook community about potential negative performance impacts of ACF and ACF’s performance vs. a competitor’s performance. Here’s the thing: ACF can be slow, just like WordPress can be slow, if you’re not taking the appropriate steps to optimize its use. Here are a few tips & tricks to optimize your ACF use.
Use Local JSON
If you’re using ACF in your projects and you’re not using local JSON, stop what you’re doing and start using this feature ASAP. It is insanely simple to implement. Create a folder in your theme called acf-json and ACF will take care of the rest as you save your field groups. If you want to go beyond basic usage and customize where the JSON is stored, you can even modify that since ACF 6.2. It’s best to drop an index.php file into the acf-json folder for security, as well.
Beyond just reducing database calls and improving performance of ACF, utilizing JSON will allow you to version control your field groups. If you do something like build custom themes and keep the theme under version control, your field groups will now be included.
If you then deploy a commit from a custom theme containing added field groups or field changes, you’ll see the option to sync those changes to the environment they were deployed to.
If you do nothing else mentioned here, please hop on the ACF JSON train.
Understand Implications of Meta Queries
While this falls more in the area of what you’re doing with ACF data than ACF itself, if you are querying posts based on a custom field value (a meta query), that database query is not a quick one. If you’re doing a complex meta query with a bunch of “LIKE” comparisons, that’s especially not a quick one. Make sure you’re not using custom fields in places where a taxonomy would be a better fit (and result in a more performant query.) Also, make sure you’re making smart choices on how you’re querying meta content. The WordPress VIP docs are actually a super helpful resource for understanding performance implications of meta queries and things to avoid. You can read their notes on meta queries here.
Be Selective with ACF’s Return Value
There are several field types, such as the ACF image field, that provide options on how ACF returns the field value, such as image array, image URL, and image ID. But in these instances, if you look at what is actually being stored as the field value, regardless of the option you choose, ACF is storing (and starting with) the ID. So if you choose to return something other than the ID, there is more work involved to return another result. Returning an image array will be slower than returning an image ID as ACF will have to assemble that array using the ID before returning it to you to do whatever you’re then doing with it on your end.
Use Repeaters Responsibly
Be mindful of the structure choices you’re making in a project as they have lasting impacts on performance. ACF repeaters are great if they’re used responsibly. But if you’re using them to display a large number of items, odds are, you’re better off with something like a custom post type.
The WordPress admin performance will degrade with a large amount of repeaters or flexible content areas so make sure you’re making the appropriate structure choices when it comes to your content.
Pay Attention to Autoload on ACF Options
ACF stores data in their options pages in the wp_options database table. Options have the ability to be autoloaded. When a WordPress site receives a request, all autoloaded options are grabbed and stored in memory, which improves the speed at which the site loads and uses these option values. You should be selective about what gets autoloaded, you don’t want to load anything that isn’t needed on the vast majority of requests, so ACF defaults to “no” as the autoload value. This makes sense as you wouldn’t want to necessarily grab every ACF options value on every page.
However, if you are setting up fields that you are pulling into every page, like say it’s a field that appears in the global header or footer, these options are better off autoloaded. You can adjust the default autoload value of options pages via acf_add_options_page.
These are just a few tips to get the most out of ACF performance. If you’re interested in diving in deeper on performance, here is a roundup of links from around the web that contain great insight to build on this topic:
- 10up is always a great resource and, while not ACF-specific, they have some great performance tips in their engineering best practices.
- The WordPress VIP documentation is bursting with great info if performance is important to you (and it should be important to everyone)
- ACF Extended takes ACF performance seriously with their performance mode, which allows developers to optimize ACF metadata and enhance database load with two different methods.