Sean Vinci Sean Vinci - 7 months ago 12
PHP Question

Get total number from multiple post object fields

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!

UPDATE/SOLUTION

Here's the solution that ended up working for me:
(based on solutions provided by hube2 on the ACF support forum.)

$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;


As mentioned above, 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 for the help!

Answer

Here's the solution that ended up working for me: (based on solutions provided by hube2 on the ACF support forum.)

$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;

As mentioned above, 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 for the help!