Home Forums ACF PRO Get total number from multiple post object fields Reply To: Get total number from multiple post object fields

  • If what you are getting from the fields is an array, then to combine the arrays you use array_merge()

    $all = array_merge($array1, $array2);

    If you are counting the number elements in an array then you use count()

    $number = count($array1);

    count() does not accept multiple arguments or multiple arrays.

    If you’re not getting the correct results than one or more of the values is not an array. For example if one of your fields has no value set it may be returning an empty string, or it may be returning NULL or false. You need to validate what you’re getting before you try to perform operations on it. You can’t assume that you will get an empty array for empty fields that will return an array when they have values.

    More then likely turning error reporting on may tell you what you’re doing wrong.

    $total = 0;
    $instructor = get_field('course_instructors');
    if (is_array($instructors)) {
      $total = count($instructors);
    // etc ...