Advanced Custom Fields – Query Efficiency

There are a couple of reasons why this generates more queries than you expect.

First, ACF stores fields as two distinct pieces of data – one part contains info about the field, what settings you selected, how to format output, etc., and the other part contains the actual value assigned to that field. So right there you have an extra query per field- the field data plus the field’s meta data.

The other reason has to do with the type of fields you have and how you’ve set the formatting options. For example, the image fields appear to have the option to load the image object selected. That means for each image reference, ACF then has to query for attachment image URLs and meta data, which produces extra queries for each image.

Should you be concerned about this?

First, it should be noted that ACF’s documentation recommends against using get_fields if you know the field names and/or won’t use all of that data on every page load, as the function uses an inefficient LIKE query to find all of the field names.

If performance becomes an issue there are a couple of things you can do – 1. use a cache plugin to reduce load, and/or 2. cache the data yourself when it changes. Hook the acf/save_post action and save all of your data as an array in a single option, then you can load it all with a single get_option query on the front end.

Leave a Comment