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. https://codex.wordpress.org/Debugging_in_WordPress
$total = 0;
$instructor = get_field('course_instructors');
if (is_array($instructors)) {
$total = count($instructors);
}
// etc ...
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.