Support

Account

Home Forums General Issues Error – count(): Parameter must be an array

Solving

Error – count(): Parameter must be an array

  • Hi,

    I am using Sage 9, WPML and ACF for a site.

    I’m trying to get the total number of post objects for a loop

    @php $totalrows = count(get_field('team_contact')) @endphp

    But I get this error:
    count(): Parameter must be an array or an object that implements Countable

    Strangely, I get there error only when I switch to the english version of my site managed by WPML. My ACF is up to date.

    Also when I wrap a conditionnal is_array() on my code, I get nothing, which indicates me that somehow “get_field(‘team_contact’)” is not considered an array.

    How can I return the total number of post objects (or rows-for repeater field) without getting this php error?

    Thanks a lot.

  • Hi,

    Having the same problem (except for the sage9 part).

    WPML + ACF – getting repeater field from a group.

    is_array($repeaterField) returns true.
    sizeof($repeaterField) and count($repeaterField) triggers a notice:

    count(): Parameter must be an array or an object that implements Countable

    Help would be highly appreciated πŸ™‚

  • This is happening to me as well. I have seen on many forums that to count the total rows within a repeater you use count(); But when I place it in gettype() it returns boolean.

    Code
    $count = count(get_sub_field('dates_locations'));
    print_r($count);

    The error output:
    Warning: count(): Parameter must be an array or an object that implements Countable in...

    Not being able to grab the total row count has been causing unnecessary problems. Any advice would be highly appreciated.

    Places that refer to getting the total number of rows from a repeater using count():

  • I’m getting the same error. Any help from the ACF team on this?

  • I cannot tell you how to do this in what you are using, but I can supply info and what I would do in PHP.

    A post object field might contain a single post ID or an array of post IDs depending on if multiple post objects were selectable when the field was saved for a specific post.

    Here’s an example of why it could be different. Add a post object field set to only allow one selection. Go to a post. ACF will save a single post ID. Go and change the field to allow muliple values and edit a different post. This field will be saved as an array, while the first post will still contain a single value.

    You need to account for several values, empty, single value, array of values.

    ACF will return a post object or array of post object unless you turn formatting off. A single post object is not countable.

    
    // get field without formatting
    $post_objects = get_field('field-name', false, false);
    See if the field is empty or not
    if (empty($post_objects)) {
      // empty, set to empty array
      // could be empty because nothing was added or because the field was never set
      $post_objects = array();
    }
    // make sure that it's an array
    if (!is_array($post_objects)) {
      $post_objects = array($post_objects);
    }
    $count = count($post_objects);
    
  • I’m not sure if we’re on the same page. My problem is specificially with ACF Repeater fields. And yes, the fields are there & have content, they are output on the page. But I cannot count them with “count()”.

    The repeater code as per ACF example:

    <?php
    
    // Check rows exists.
    if( have_rows('repeater_field_name') ):
    
        // Loop through rows.
        while( have_rows('repeater_field_name') ) : the_row();
    
            // Load sub field value.
            $sub_value = get_sub_field('sub_field');
            // Do something...
    
        // End loop.
        endwhile;
    
    // No value.
    else :
        // Do something...
    endif;

    Now if I add count “$count = count(get_field(‘repeater_field_name’) );” , it doesn’t work:

    <?php
    
    // Check rows exists.
    if( have_rows('repeater_field_name') ):
    
    $count = count(get_field('repeater_field_name') );
    
        // Loop through rows.
        while( have_rows('repeater_field_name') ) : the_row();
    
            // Load sub field value.
            $sub_value = get_sub_field('sub_field');
            // Do something...
    
        // End loop.
        endwhile;
    
    // No value.
    else :
        // Do something...
    endif;

    My question – what code do I need to count how many repeats I have?

  • You need to get and count a repeater before you loop over the repeater with have_rows(). Trying to count the rows while inside the have_rows() loop for that repeater will have unexpected results.

    
    $count = count(get_field('repeater_field_name') );
    if (have_rows('repeater_field_name') {
      // code continues
    

    For this reason you need to test the repeater before you try to count it.

    
    $count = 0;
    $repeater = get_field('repeater_field_name');
    if (!empty($repeater)) {
      $count = count($repeater);
    }
    if (have_rows('repeater_field_name') {
      // code continues
    
  • Thank you very much John! Finally got it πŸ™‚

    My mistake was to assume have_rows() is equivalent to “is not empty” or “exists” so I didn’t appreciate that placing my code inside have_rows() would not work.

    Got it & works perfectly now, thank you!

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

You must be logged in to reply to this topic.