Home Forums General Issues Optimize get_field for one database query


Optimize get_field for one database query

  • I have many fields for every post which in turn results in many database queries, and I am trying to optimize that since my server is barely holding up. Is there a way to get all fields for the post in one query?
    I am currently using the following function, but I have tried with get_fields and number of queries in the end is the same

    function get_data(){

    $acf = array(‘var1’, ‘var2’, ‘var3’, ‘var10’, ‘var11’, ‘var15’…);

    $data = array();
    foreach($acf as $val){
    $data[$val] = ob_get_contents();

    return $data;

  • You can cause WP to get all the custom fields for a post and then cache them, once this is done additional database calls are not made and instead the values from the cache are returned. To do this you use get_post_meta() without giving a meta_key

    $meta = get_post_meta($post->ID);

    once you do this you can ignore what is in $meta and use get_field(), or you can use the array that’s returned instead.

  • Hm it didn’t matter if I called get_post_meta before, the number of queries was the same. But I managed to solved it like you mentioned, by using variables from get_post_meta array.

  • Sometimes it works, but are you saying that if you use the array returned form it then the number of queries is reduced? That’s not the way it’s supposed to work in theory.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.