Hello!
I’ve gone back and forth on the proper way to do this and was wondering if you had some insight as to what is less resource intensive.
If I want all / almost all of the fields for a page, does it make more sense to do a get_fields() call and access my info from that array or should I be doing 7 get_field(‘fieldxxx’) calls? Basically does calling get_field multiple times have a negative impact compared to calling get_fields() once and using that array?
If you are seeing a different number of queries being done when using get_field() as apposed to get_fields(), there are some things you can to.
The first thing is to call get_post_meta($post->ID)
, this will cause all of the meta values for a post to be loaded into the WP cache and then any additional calls will not create additional queries.
calling get_fields()
will cause the same thing to happen as the first option.
Once this is done you don’t really need to use the array that’s returned, you can just use get_field('field_name')
after that and these will get values from the cache and not do an additional query.
I’m not really sure why you’d be seeing a difference in the number of queries unless you’re getting values from another post, not the current post in “The Loop”. Usually, I’ve found that when setting up a post WP automatically gets all the post meta values automatically.
Hi John,
Thanks for the response!
So does that mean NOT calling get_fields()
first and then calling multiple get_field()
would make more DB calls as it would not load the meta in to the cache? I just want to make sure I completely understand the differences.
Thanks!
Calling get_fields() can reduce the number of queries done. This does one DB query to get all meta values and WP caches the results of the query.
Once you use get_fields() it does not matter after this if you use get_field() to get each field value because this will not cause any more queries to be done.