Include Child Posts in Loop in CPT With Hierarchy

I posted the other day about an issue with the output of a loop that queried a radio button, but after a bit of digging around I’ve realised that it’s actually a different issue that relates to post hierarchy in a CPT.

We have a CPT that uses a hierarchy. When we query it within a loop it will only show the top level parents. Is there any way to include the child posts in this loop?

Hi Matthew,

You need the children=true in your loop.

Here is an example from my own site:

 <Loop type="taxonomy_term" taxonomy="gallery_category" children="true" hide_empty="true">
...
</Loop>

It’s covered in the docs here:

Hi Phil,

Thanks for the reply. I did actually try that but it didn’t seem to help. This is a post loop rather than a taxonomy_term loop, which may make a difference?

Slimming it down to the most basic form of the template it looks like this:

<ul>
  <Loop type="custom_cpt" orderby="title" order="asc">
    <li>
        <a href="{Field url}"><Field title /></a>       
    </li>
  </Loop>
</ul>

The result is the same if you include children=“true”, eg:

<ul>
  <Loop type="custom_cpt" orderby="title" children="true" order="asc">
    <li>
        <a href="{Field url}"><Field title /></a>       
    </li>
  </Loop>
</ul>

I think I’ve figured it. I couldn’t find it mentioned in the documentation, but this appears to work:

include_children=true

Oh yeah, apologies. I thought you were using hierarchical CPT taxonomies. The previous example will work when a post loop is added inside the taxonomy term loop but that’s another scenario. :slight_smile:

1 Like