• Resolved flomei

    (@flomei)


    I’m using get_fields in a loop like this:

    foreach($frontpage_posts as $post) : ?>
    
       <?php
          // do foo here
          $timetoshow = '';
          if($cat[0]->slug === 'some_slug') {
             $acf = get_fields($post->ID);
             $timetoshow = $acf['my_field'];
          } else {
             $timetoshow = date('d.m.Y', strtotime($post->post_date));
          }
       ?>
    
       <!-- html output here... -->
    
    <?php endforeach; ?>

    When doing that, it crashes with the following error:

    Fatal error: Uncaught Error: Call to a member function init() on array in /my/host/wp-content/plugins/advanced-custom-fields/includes/api/api-helpers.php:209

    Stack trace:
    #0 /my/host/wp-content/plugins/advanced-custom-fields/includes/api/api-template.php(310): acf_init()
    #1 /my/host/wp-content/plugins/advanced-custom-fields/includes/api/api-template.php(269): get_field_objects(85, true)
    #2 /my/host/wp-content/themes/fwrhs/index.php(77): get_fields(85)
    #3 /my/host/wp-includes/template-loader.php(106): include(‘/var/www/vhosts…’)
    #4 /my/host/wp-blog-header.php(19): require_once(‘/var/www/vhosts…’)
    #5 /var/ww in /my/host/wp-content/plugins/advanced-custom-fields/includes/api/api-helpers.php on line 209

    The post looks just fine to me, any ideas what might cause this?

    • This topic was modified 4 years, 5 months ago by flomei.
    • This topic was modified 4 years, 5 months ago by flomei. Reason: format code
Viewing 1 replies (of 1 total)
  • Plugin Contributor Elliot Condon

    (@elliotcondon)

    Hi @flomei

    Thanks for the question,
    Elliot here – ACF dev???.

    Be aware that using the variable $acf in some environments can actually overwrite the global ACF instance. This is what your issue is.

    Please use a different variable name such as $fields.

    Cheers
    Elliot

Viewing 1 replies (of 1 total)
  • The topic ‘Plugin crashes when get_fields is called in loop’ is closed to new replies.