Loop inside of a loop. Don't show item from first loop if no posts in inside loop

So I have two Loops. First loop is a Taxonomy loop (States), and inside that is a Loop (Store) for all locations inside that State (but only if they match another taxonomy - Rewards).

<Loop type=taxonomy_term taxonomy=location_state>
    <h2><Field title /></h2>
    <Loop type=store taxonomy=rewards terms="{Field id}">
        <a href="{Field url}"><Field title /></a>
    </Loop>
</Loop>

But sometimes there are no items inside the second loop because not all Stores have Rewards enabled.

So how can I remove the <h2> if its loop below it doesn’t have any results?

I’m assuming an <If> statement of some kind but can’t quite figure out how to do it for something that lives outside of the loop you’d be checking in…

I kinda thought I would be able to do something like this:

<Loop type=taxonomy_term taxonomy=location_state>
    <If loop exists type=store taxonomy=rewards terms="{Field id}">
        <h2><Field title /></h2>
        <Loop type=store taxonomy=rewards terms="{Field id}">
            <a href="{Field url}"><Field title /></a>
        </Loop>
    </If>
</Loop>

But that didn’t work - it output nothing

And if I do this, it outputs nothing:

<Loop type=taxonomy_term taxonomy=location_state>
    <If loop exists type=store taxonomy=rewards terms="{Field id}">
        <h2><Field title /></h2>
        <Loop>
            <a href="{Field url}"><Field title /></a>
        </Loop>
    </If>
</Loop>

And if I do this like the documentation says I should, then it just outputs every post under every State:

<Loop type=taxonomy_term taxonomy=location_state>
    <If loop exists type=store taxonomy=rewards>
        <h2><Field title /></h2>
        <Loop terms="{Field id}">
            <a href="{Field url}"><Field title /></a>
        </Loop>
    </If>
</Loop>

That would maybe lead me to believe it is a bug that this doesn’t work?

<If loop exists type=store taxonomy=rewards terms="{Field id}">

This is another option to test to:

<If loop exists type=stores taxonomy=rewards parent="{Field id}">

Hmm… So I just realized that this from the beginning doesn’t work… So I for sure need to get that to work first before testing anything else

<Loop type=taxonomy_term taxonomy=location_state>
    <h2><Field title /></h2>
    <Loop type=store taxonomy=rewards terms="{Field id}">
        <a href="{Field url}"><Field title /></a>
    </Loop>
</Loop>

This DOES work (below), so idk why changing the taxonomy on the inner Loop (in my example above) doesn’t work?

<Loop type=taxonomy_term taxonomy=location_state>
    <h2><Field title /></h2>
    <Loop type=store taxonomy=location_state terms="{Field id}">
        <a href="{Field url}"><Field title /></a>
    </Loop>
</Loop>

I’m going to open up a separate thread to discuss this part first: Taxonomy loop with post loop inside (but pulling another taxonomy)?

So with the basics addressed in your separate post, the solution to hide the heading if there are no matching items in the loop looks like this:

<Loop type=taxonomy_term taxonomy=location_state>
  <If loop type=store taxonomy=location_state terms="{Field id}" taxonomy_2=rewards taxonomy_compare_2=exists terms_2="">
    <h2><Field title /></h2>
    <Loop type=store taxonomy=location_state terms="{Field id}" taxonomy_2=rewards taxonomy_compare_2=exists terms_2="">
        <a href="{Field url}"><Field title /></a>
    </Loop>
  </If>
</Loop>

Perfect. Thank you so much

1 Like