ACF best practice – multiple get_field vs one get_fields

In a case where a post have a few dozen custom fields and I just need two or three, in term of performance and optimisation, is it better to do one request getting every fields with

get_fields()

Read More

or multiple requests getting a single field with

get_field($field_name)

and if it depends (on the post’s total number of fields, the number of fields needed, etc), at which point does one solution become better than the other ?

Related posts

1 comment

  1. Each time you use get_field(), you create a new query to the database. So, generally speaking, it would be a good idea to use get_fields() first, to cache all the results, then get_field() where needed.

    For example:

    get_fields();
    
    $field1 = get_field('field1');
    $field2 = get_field('field2');
    

    Still, if you have lots of fields, with large data in them, and you only need two of them, it might be better to just use get_field() twice, instead of increasing the load of the DB server and filling the cache with useless data.

Comments are closed.