How to create archive page for taxonomy terms within custom post type

I am needing to create an archive page custom taxonomy terms that are within a custom post type. It’s set up as CPT > Custom Taxonomy > Taxonomy Terms.

I have a custom post type called “Projects,” and when creating the post, the user is to select a type of project for the listing (i.e. Construction, Maintenance, etc.). But, I need to create archive pages for the individual terms. I’ve been searching and working at it via trial-and-error for hours and just keep banging my head into the wall.

Solutions Collecting From Web of "How to create archive page for taxonomy terms within custom post type"

There is an example in the WP Codex which should work for you:

Try the following naming convention for your taxonomy term archive template:

taxonomy-{taxonomy}-{term}.php

So, let’s say you have a CPT named “Projects,” a taxonomy named “Maintenance,” and a term within the taxonomy named “Professional.” Then your naming convention would be:

taxonomy-maintenance-professional.php

First, WordPress does not offer any built-in solution to displaying posts of a specific post type and a specific taxonomy term. I’m talking about something like get_term_archive_link_for_post_type( $term, $post_type ) or so.

So you have to do this yourself.

Of course, you could create a page, name it Construction Projects, give it a new page template, in which you set up a custom query and display exactly what you want to. But that’s not even necessary.


WordPress can handle several query vars—one of these is the one your taxonomy creates.

Suppose you have a Custom Post Type with the slug projects.
Then you have a Custom Taxonomy having the slug project_type.
And you have a term with the slug construction in this very taxonomy.

Then visiting ?post_type=projects&project_type=construction will display an archive of all Projects having (at least) Construction set as Project type.

WordPress will interprete this query as a Term archive and hence rewrite the URL according to your permalink settings, and attach the post type as query var to the URL. In the usual case, this would lead to /project_type/construction?post_type=projects.

Note: You can explicitly alter the query var for a custom taxonomy. In that case, using the taxonmy slug as query var (e.g., project_type=construction) won’t work any longer. You have to use the query var that you explicitly defined using register_taxonomy then.


And here is some example of setting up a link list for all terms of a specific taxonomy:

$args = array(
    'orderby'    => 'name', 
    'order'      => 'ASC',
    'hide_empty' => TRUE, 
    'fields'     => 'all', 
); 
$terms = get_terms( 'project_type', $args);

if (
    ! is_wp_error( $terms )
    && ! empty( $terms )
) {
    echo '<ul>';

    foreach ( $terms as $term ) {
        $url = get_term_link( $term );
        if ( is_wp_error( $url ) ) {
            continue;
        }

        $url = add_query_arg( 'post_type', 'projects', $url );
        printf(
            '<li><a href="%s">%s</a></li>',
            $url,
            $term->name
        );
    }

    echo '</ul>';
}