Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Count # of instances & rows in ACF Nested Repeater WordPress

  • SOLVED

Hello,

I'm currently working with a nested repeater within ACF for a questionnaire template. I've been banging my head against the wall on this for a few days and this has to be something I'm overlooking.

The situation is as follows:
I have one repeater field called "responses" which contains a group name field and another repeater (called "responsegroup") which houses fields for both a question and a response (select field of null, yes, or no).

I'm trying to figure out how to count the total number of sub-rows if you will and the total number of "Yes" responses. These are numbers that I need complete counts of (not by the individual question groups). I've played a few solutions I've seen for counting rows on the ACF forums and stackoverflow, but it seems I only end up with a total count of the # of groups (not # of individual row responses).

The goal here is that I could say at the top of the page "Supports #A(total "Yes") of #B(Total sub-rows across all groups) reforms"

(Template in progress)
http://dev-dem2020.pantheonsite.io/candidate/joe-biden/

Any help would be greatly appreciated.

Answers (3)

2019-05-15

Andrea P answers:

If you want to count the values rather than the amount of selector-fields, then you need to do it during a loop, you can't just count the repeater rows.
Well you could to count the total of items, but then you also need to know how many of them are yes or no.

My suggestion would be to simply add them up during the loop to a couple of variables that you declare on top (i.e. $total_yes $total_no and $total). So while you're printing them out you also count them. Then at the end of the loop you can print the results out.

As you need to show these totals on the top, you could do another loop there just to count, or (better) write a little javascript which would populate some empty divs on top of the page with the values of those at the bottom when the page loads.

2019-05-15

Cesar Contreras answers:

with JS you can get that result with the following code

//all the answers "not"
$(".responserow>.atabled>.cell.two>p.race-results__response.race-results__response--no").length;

//all the answers "yes"
$(".responserow>.atabled>.cell.two>p.race-results__response.race-results__response--yes").length;

2019-05-15

Arnav Joy answers:

Is it possible you can show me all these live in action?


Andrew Clemente comments:

Sure:
example - http://dev-dem2020.pantheonsite.io/candidate/joe-biden/