Loop=previous_total does not account for If statement

I have an acf_relationship field that holds two types of CPT. I have successfully been able to output data using

<If field=type value="wspid">
<Field custom_field_name />
</If>

Part of my journey is creating a circle with the total number of related posts of a certain type.

I have tried lots of different combinations but they all seem to output the loops total not taking into account the If statements.

For example

<Loop acf_relationship=client_linked_spids>
  <If field=type value="wspid">
    </If>
</Loop>
<div class="wspid-circle">
<Get loop=previous_total />
</div>

The above from my dataset displays 46 (which is the total number related posts) - however with the IF it should show 1.

I have also tried, but it didn’t work:

<Loop acf_relationship=client_linked_spids type=wspid>

I also tried using SET with the GET inside the IF but that didn’t work either

Any thoughts would be appreciated

<Get loop=previous_total /> will always reflect the total number of posts in the loop, even if those posts are being hidden with logic. You could either modify the loop to also filter by your custom field so that the number of posts displayed matches the number of posts returned by the loop, or you can get the total number of posts by incrementing a variable inside your <If> rule.

<Loop acf_relationship=client_linked_spids custom_field=type custom_field_value=wspid>
</Loop>
<div class="wspid-circle">
  <Get loop=previous_total />
</div>

or

<Set loopTotal>0</Set>
<Loop acf_relationship=client_linked_spids>
    <If field=type value="wspid">
        <Set loopTotal><Math><Get loopTotal /> + 1</Math></Set>
    </If>
</Loop>
<div class="wspid-circle">
  <Get loopTotal />
</div>

Let me know if that helps!

Thanks Julia :grinning:

The first solution didn’t work, but the incremental variable solution did

1 Like