I have a custom post type with a hierarchical custom taxonomy. The taxonomy has several parent terms and each parent term has several child terms. The posts are assigned to one parent and (mostly) one child.

I want that my archive page example.com/{cpt_slug} only displays the parent terms. After a first selection, the user will be on the second archive page example.com/{cpt_slug}/{parent_term_sug}. If the selected term has children, this page should only display those, so that the user can, once again, take a decision on what he wants to see. Otherwise, the posts of this term should be shown.

At the moment there are no plans to have child-child-terms, but if it's possible to have a completely dynamic code which could handle grandchildren and maybe even more, that would be even better.

Have you any idea on how I could achieve this? Many thanks in advance!!

  • Following your description, if you have a post which is only assigned to a parent term which has child terms, but the post isn't assigned to any child terms, the post will not appear on any archive page.
    – JHoffmann
    Jan 26, 2017 at 23:40
  • @JHoffmann This won't happen. It wouldn't be logical, because the nature of our website. If there are child terms, the posts have to be assigned to them.
    – Sam
    Jan 26, 2017 at 23:45
  • I just wanted to raise that point in case you were not planning to prevent that.
    – JHoffmann
    Jan 26, 2017 at 23:55
  • @JHoffmann No problem :)
    – Sam
    Jan 26, 2017 at 23:57
  • I tried to register a post type and a taxonomy sharing a common base slug, for me this didn't work (I got a 404 for the taxonomy archive). Having the terms show up under example.com/{cpt_slug}/{parent_term_sug} urls is probably not going to be easy to implement. Maybe with a plugin like Custom Post Type Permalinks (I haven't tested this).
    – JHoffmann
    Jan 27, 2017 at 0:27

1 Answer 1


To make your cpt archive page show a list of your parent terms you can create a template file in your theme with the name archive-{cpt_slug}.php. This file will automatically be picked up when your cpt archive is called. There instead of writing a loop to output the individual posts you fetch a list of the parent terms to output them. Try to retrieve the terms like this:

$terms = get_terms( array(
    'taxonomy' => 'custom_taxonomy',
    'parent' => 0,
) );

These terms you link to the respective taxonomy archive page of the terms. For these pages now you create a taxonomy archive template named taxonomy-{custom_taxonomy}.php. Here you fetch the child terms (change 'parent' => 0, in the above query to 'parent' => get_queried_object_id(),). If this yields an empty result just start a loop to output the posts normally, else output the found child terms.

Just keep in mind, that the first archive is the archive for your post type (normally the slug would be: /{cpt_slug}/), the second template is for the custom taxonomy (here the normal slug would be: /{custom_tax_slug}/{term_slug}/) and the linked posts are again under the cpt's slug (normally /{cpt_slug}/{post_slug}/).

If you need samples how the template files should look, check your current theme's files an look for the archive.php and if present taxonomy.php or category.php. These files should be a good starting point.

  • Okay, so I would have to create a .php file for every child therm? That's not very convenient, but if it's the only method, I'll have to do it :/ How do I link to them?
    – Sam
    Jan 27, 2017 at 13:00
  • Oh and where do I display the posts, if there are child terms? You said, that you would output the parent terms in the CPT-archive and the child terms (or the posts, if there are no child terms) on the CT page. But which file is used to display a listing of all the posts that are assigned to a parent and a child term?
    – Sam
    Jan 27, 2017 at 13:07
  • @Chwebo In your question you wrote that if a term has children, the page for the term should only show these, the posts would then be shown on the leaves in the term tree (the terms without child terms). As you confirmed there wouldn't be any posts assgned to parent terms without also being assigned to one of its child terms. The file doing this would be taxonomy-{custom_taxonomy}.php as stated above, either outputting child terms if present, else the posts associated to the fetched term.
    – JHoffmann
    Jan 27, 2017 at 13:30
  • Yeah that's ok. The thing I don't understand is how I output the pages that are assigned to a parent and a child. What you're describing is a page, that checks if there are any terms and if not, displays the posts. That's fine. But what if there are terms. Then they are output. To wich page do these child terms link? Or in other words which page outputs the pages that have a parent and a child term?
    – Sam
    Jan 27, 2017 at 13:33
  • @Chwebo The link would be the one generated by get_term_link() which is for the term archive page. On this page the file taxonomy-{custom_taxonomy}.php is automatically used as template if present. It's just as easy as that.
    – JHoffmann
    Jan 27, 2017 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.