Advanced Custom Fields (ACF) is an amazing plugin that can revolutionize the way you build and edit posts inside WordPress. Two of the more common ACF features are repeater fields and flexible content fields, which can make the post editing experience much more intuitive. In fact, I can’t recall the last website I build where I did not use ACF repeater or flexible content fields.
ACF is great, but…
There are a couple of things about ACF that leave something to be desired, one of which is the function
get_field(). This function is extremely convenient when working with repeater and flexible content fields because it retrieves all of the pertinent meta values and puts them into a nice multidimensional array; however, it is also expensive when it comes to performance.
In a classic post by Bill Erickson, he recommended not using ACF functions like
get_field() largely because of poor performance. Bill recommends using the native WordPress function
get_post_meta() to retrieve post meta values because it’s faster and won’t result in the dreaded white screen of death that could happen if/when ACF is deactivated. Craig Simpson also did a nice writeup about performance considerations when working with ACF, along with the numbers to prove it.
WET Code Stinks
Now there’s a slight problem with using
get_field(): it can be a bit of a pain to write the functions that retrieve the post meta values for all those repeater subfields and flexible content layouts.
What’s worse is that because these ACF field groups are often tailored for each client, you usually end up rewriting these functions on every project. And WET (Written Every Time) code is something that no developer is proud of.
Is There a Better Way?
In a recent project I challenged myself to find a better way to get the ACF post meta values. I was hoping to come up with a function that could be called once and return a multidimensional array with all the meta values that I needed. In essence, I was looking for a direct replacement for
This is the recursive function that I came up with:
How to Implement
get_all_custom_field_meta() has two required parameters: the post ID, and a config array. The config array must have the same structure as the ACF code for exporting/importing field groups.
1) Manual way: Copy the
fields array from ACF’s field group export or condense it down like this. Either will work.
2) Automated way: Once you set up ACF Pro’s Local JSON feature you never have to remember to update the config. Just include the entire field group configuration JSON (which is always the latest verstion), decode it to an array, and pass the array to
get_all_custom_field_meta(). This is what it looks like:
And here is an example array that is returned by calling
get_all_custom_field_meta( get_the_ID(), $config ):
Hurray for DRY Code!
Yes, I think there is a better way than relying on
get_field() or continually rewriting functions to retrieve ACF repeater and flexible content field values. The function
get_all_custom_field_meta() is flexible, meaning your code stays DRY (Don’t Repeat Yourself), plus it returns all of the post meta values in a nice multidimensional array.
Oh, and it’s fast too. I ran some basic performance tests and
get_all_custom_field_meta() was 50-100 times faster than
get_field() in the example provided here (and 300+ times faster in other tests that I ran). The relative performance gap widens (gets better) as the config array increases in complexity because the function is doing far less work than
Give it a whirl!