Advanced Custom Fields – Query Efficiency

For testing purposes I have edited page.php to have nothing other than:

<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php $site_options = get_fields('options'); ?>
<?php endwhile; ?>

I removed get_header, get_footer and only have ACF plugins enabled because I am still early in development and doing some initial testing. When I view the page without calling get_fields it reports 9 queries but if I visit the page with get_fields it jumps to 72 queries. It seems like that should only bump by 1 or 2 queries…not 63.

Read More

Queries are reported using:

get_num_queries();

Results from get_options call:

Array(
[main_ad_-_image] => Array
    (
        [id] => 25
        [alt] => TEST
        [title] => lg-social_media
         => 
        [description] => 
        [mime_type] => image/png
        [url] => {urlhere}
        [width] => 460
        [height] => 260
        [sizes] => Array
            (
                [thumbnail] => {urlhere}/wp-content/uploads/lg-social_media-150x150.png
                [thumbnail-width] => 150
                [thumbnail-height] => 150
                [medium] => {urlhere}/wp-content/uploads/lg-social_media-220x124.png
                [medium-width] => 220
                [medium-height] => 124
                [large] => {urlhere}/wp-content/uploads/lg-social_media.png
                [large-width] => 460
                [large-height] => 260
            )

    )

[main_ad_-_url_type] => ext
[main_ad_-_url] => www.google.com
[small_ad_1_-_image] => Array
    (
        [id] => 28
        [alt] => Fake Text
        [title] => fake-text
         => 
        [description] => 
        [mime_type] => image/png
        [url] => {urlhere}/wp-content/uploads/fake-text.png
        [width] => 220
        [height] => 120
        [sizes] => Array
            (
                [thumbnail] => {urlhere}/wp-content/uploads/sm-fake-text-150x120.png
                [thumbnail-width] => 150
                [thumbnail-height] => 120
                [medium] => {urlhere}/wp-content/uploads/sm-fake-text.png
                [medium-width] => 220
                [medium-height] => 120
                [large] => {urlhere}/wp-content/uploads/sm-fake-text.png
                [large-width] => 220
                [large-height] => 120
            )

    )

[small_ad_1_-_url_type] => ext
[small_ad_2_-_image] => Array
    (
        [id] => 30
        [alt] => Fake Text 2
        [title] => fake-text2
         => 
        [description] => 
        [mime_type] => image/png
        [url] => {urlhere}/wp-content/uploads/fake-text2.png
        [width] => 220
        [height] => 120
        [sizes] => Array
            (
                [thumbnail] => {urlhere}/wp-content/uploads/fake-text2-150x120.png
                [thumbnail-width] => 150
                [thumbnail-height] => 120
                [medium] => {urlhere}/wp-content/uploads/fake-text2.png
                [medium-width] => 220
                [medium-height] => 120
                [large] => {urlhere}/wp-content/uploads/fake-text2.png
                [large-width] => 220
                [large-height] => 120
            )

    )

[small_ad_1_-_url] => www.google.com
[main_ad_-_page] => {urlhere}
[small_ad_2_-_url_type] => ext
[small_ad_2_-_url] => www.google.com
[small_ad_3_-_image] => Array
    (
        [id] => 27
        [alt] => Fake Text 3
        [title] => fake-text3
         => 
        [description] => 
        [mime_type] => image/png
        [url] => {urlhere}/wp-content/uploads/fake-text3.png
        [width] => 220
        [height] => 120
        [sizes] => Array
            (
                [thumbnail] => {urlhere}/wp-content/uploads/fake-text3-150x120.png
                [thumbnail-width] => 150
                [thumbnail-height] => 120
                [medium] => {urlhere}/wp-content/uploads/fake-text3.png
                [medium-width] => 220
                [medium-height] => 120
                [large] => {urlhere}/wp-content/uploads/fake-text3.png
                [large-width] => 220
                [large-height] => 120
            )

    )

[small_ad_3_-_url_type] => ext
[small_ad_3_-_url] => www.google.com
[small_ad_4_-_image] => Array
    (
        [id] => 29
        [alt] => Fake Text 4
        [title] => fake-text4
         => 
        [description] => 
        [mime_type] => image/png
        [url] => {urlhere}/wp-content/uploads/fake-text4.png
        [width] => 220
        [height] => 120
        [sizes] => Array
            (
                [thumbnail] => {urlhere}/wp-content/uploads/fake-text4-150x120.png
                [thumbnail-width] => 150
                [thumbnail-height] => 120
                [medium] => {urlhere}/wp-content/uploads/fake-text4.png
                [medium-width] => 220
                [medium-height] => 120
                [large] => {urlhere}/wp-content/uploads/fake-text4.png
                [large-width] => 220
                [large-height] => 120
            )

    )

[small_ad_4_-_url_type] => ext
[small_ad_4_-_url] => www.google.com
[facebook] => www.facebook.com
[twitter] => twitter.com
[youtube] => www.youtube.com
[office_address] => text
[office_address_-_google_url] => test
[office_-_phone] => 1234567890
[office_-_800_#] => 8001234567
[office_-_email] => email@email.com)

Should I be concerned about this?

Related posts

1 comment

  1. 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.

Comments are closed.