Support

Account

Home Forums Front-end Issues How to get all ACF fields in one request

Solving

How to get all ACF fields in one request

  • Hi. I can not solve one problem. I would be grateful for any help )

    I get a list of rubrics through the get_categories function.

    $categories = get_categories( array(
      'type'         => 'post',
      'child_of'     => '',
      'parent'       => '',
      'orderby'      => 'id',
      'order'        => 'DESC',
      'hide_empty'   => 0,
      'hierarchical' => 1,
      'exclude'      => '',
      'include'      => '',
      'number'       => 0,
      'taxonomy'     => 'category',
      'pad_counts'   => false,
      'fields'       => 'all'
    ));

    Next, I show everything in the loop. Plus, I display the image added through the ACF field.

    <?php if($categories){foreach( $categories as $cat ){ ?>
      <?php
        $img = get_field('icon_rubrica', 'category_'.$cat->term_id);
      ?>
      <img src="<?php echo $img['url']; ?>" />
    <?php }}?>

    Everything is working. But the get_field code creates a lot of additional requests in the database due to the fact that it works in a loop. For each iteration of the loop, it creates an additional 3 queries to the database.

    Is there any way to get all the required fields at once, don’t use a cycle? With one overall request?

  • This is a limitation of WP. get_field() when used to get custom fields for a term is wrapper for get_term_meta() https://developer.wordpress.org/reference/functions/get_term_meta/ or more precisely get_metadada('term', ....). WP does not provided any facility to get a meta value from multiple terms at the same time.

  • Thank you very much for the answer. I did not think that the problem is so global at the wordpress level.

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

The topic ‘How to get all ACF fields in one request’ is closed to new replies.