Support

Account

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

  • 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);