Support

Account

Home Forums Add-ons Repeater Field ACF Repeater returns count of field in Search Loop

Solving

ACF Repeater returns count of field in Search Loop

  • When using an ACF repeater in v. 5.3.2.2, get_field() is returning an integer that contains the number of rows in that field instead of an array of the data in that field.

    This is only happening on the search page, the archive page which relies on the same code works fine, returning an array, so it seems that it has something to do with the_loop being called from search.php.

    The exact code is as follows:

            $authors = get_field('authors', $post->ID);
            if($authors) {
                foreach($authors as $author) :
                    $author = array_pop($author);    
                    $author_string .= $author['display_name'].', ';
                endforeach; 
                echo "<p>By " . substr($author_string,0,-2) . " | Posted on " . get_the_date() . "</p>"; 
            } 

    This shows the authors properly on the archive page, but when searching throws the following error:

    Warning: Invalid argument supplied for foreach() in /home/themes/my-theme/excerpt/excerpt-default.php on line 47

    Echoing out $authors at that line reveals an integer corresponding to the number of rows in the authors ACF field.

  • While I understand what you’re doing, it’s not really a good idea to depend on ACF returning an array of rows. Over the years I’ve been using ACF I’ve seen it change back and forth in places. I’ve coded for an array only to have sites break after an update because it was not longer returning an array in that place.

    I’ll mark this for the developer to look at if he can. Since you marked this as the repeater add on I’m assuming you’re using ACF4, which isn’t really being updated except for bugs and changes to WP the make it required.

    Your best bet is to use have_rows and get_sub_field whenever dealing with repeaters. http://www.advancedcustomfields.com/resources/have_rows/

  • I’ve had the same issue. This is probably because you haven’t added the field group before trying to get the field.

    please post the (simplified) code where you added the field group so we can validate.

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

You must be logged in to reply to this topic.