Support

Account

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

Solved

Get total number from multiple post object fields

  • Hello,

    I’m pretty new to PHP so my question might have a very simple answer, but I’ve searched the ACF forum and Google with no luck. Hopefully someone here can help.

    I have four multi-select post object fields on my page and I’m attempting to get the total number of posts (or in my case staff) in those post objects. I’d like to combine them somehow so I can use the total with a conditional.

    I’m able to get the amount for a single post object using count().

    <?php
    
    // vars
    $instructor = get_field('course_instructors');
    
    if (count($instructors) > 1) {
      dosomething...
    }
    
    ?>

    but trying to add them together within count() doesn’t work.

    <?php
    
    // vars
    $instructor = get_field('course_instructors');
    $leaders = get_field('course_leaders');
    $designers = get_field('course_designers');
    $speakers = get_field('course_speakers');
    
    if (count($instructors + $leaders + $designers + $speakers) > 1) {
      dosomething...
    }
    
    ?>

    I’ve also tried array_merge() and other array functions with no luck, but I’m not 100% sure that the output of a post object is an array…although it looks like it when I use print_r().

    Ideally my code would work something like this:

    <?php
    
    // vars
    $instructor = get_field('course_instructors');
    $leaders = get_field('course_leaders');
    $designers = get_field('course_designers');
    $speakers = get_field('course_speakers');
    $all_staff = $instructors + $leaders + $designers + $speakers;
    
    if (count($all_staff) > 1) {
      dosomething...
    }
    
    ?>

    When I do that I get an error: “Fatal error: Unsupported operand types in…”.

    Hopefully someone can answer this for me or at least point in the right direction.
    Thanks in advance. Much appreciated!

  • You can’t add arrays together, you need to add the count values of each array

    
    $total = count($instructors)+count($leaders)+count($designers)+count($speakers);
    
  • @hube2 Thanks for your response. I tried your solution, but it doesn’t seem to be working. I used print_r() to see what value I was getting and it’s giving me the number of arrays (4) rather than the total number of objects in the 4 arrays. It’s very strange since I’m able to get the correct number if I use count() on an individual post object.

  • 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. https://codex.wordpress.org/Debugging_in_WordPress

    
    $total = 0;
    $instructor = get_field('course_instructors');
    if (is_array($instructors)) {
      $total = count($instructors);
    }
    // etc ...
    
  • @hube2 You were right – my empty fields were returning false. I was able to use your method of validating with some slight tweaks to work for my specific case.

    Here’s the solution that ended up working for me:

    $instructors_total = 0;
    $instructors = get_field('instructors');
    if (is_array($instructors)) {
      $instructors_total = count($instructors);
    }
    $leaders_total = 0;
    $leaders = get_field('leaders');
    if (is_array($leaders)) {
      $leaders_total = count($leaders);
    }
    $designers_total = 0;
    $designers = get_field('designers');
    if (is_array($designers)) {
      $designers_total = count($designers);
    }
    $speakers_total = 0;
    $speakers = get_field('speakers');
    if (is_array($speakers)) {
      $speakers_total = count($speakers);
    }
    $staff_total = $instructors_total + $leaders_total + $designers_total + $speakers_total;

    This is based on both of @hube2’s recommendations except that I’m using count() inside my is_array() validations rather than outside of them. Using count() to add the totals together outside the validation was returning a ‘1’ even if the array was empty. So if all of my arrays were empty, I was still getting ‘4’. I’m not entirely sure why.

    There might be a better way to do this, but this is working well for me.

    Thanks again @hube2!

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

The topic ‘Get total number from multiple post object fields’ is closed to new replies.