If you’ve ever worked on a site with many ACF fields and it hasn’t had ACF JSON enabled, you might find it has to run a lot more database queries than you would expect. This is basically because ACF needs to look up each field in the database in order to then look up the field value which involves additional SELECT
queries.
You can cut down on these extra database queries by enabling ACF JSON. When ACF boots, it looks for an acf-json
directory within the current active theme. If it finds this directory, it will load up all .json
files within and attempt to load the contents of each as field groups and their fields into a map. With the map in place, ACF can avoid unnecessary database calls as it has a reference for all field keys and names. ACF uses the map to understand which field value it needs to find when you make a call such as get_field('my_field')
.
Enabling ACF JSON is as simple as creating creating an /acf-json
directory inside your active theme. ACF checks to see if the directory exists when you save a field group and, if it does, it saves a .json
file containing the field group and all its fields.
Saving multiple field groups at once can be a slow process
If you have a site with a lot of field groups, opening each and saving them can be a bit of a pain. Fortunately, we can utilise some convenient functions within ACF and basically generate field group JSON files for all field groups in a few simple lines of code:
This line of code might be a bit difficult to run on its own so you’ll want to place it in some script or process you can execute. A nice, easy way to do this can be to use an admin post endpoint:
The above script will basically be runnable by logged in users and can be run by simply visiting yoursite.com/wp-admin/admin-post.php?action=acf_sync
in your browser.