2

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.

4
  • The problem with that is it brings up an archive for ALL the taxonomy terms...I need three separate archives for three separate taxonomy terms. Dec 12, 2014 at 17:42
  • Not sure what kind of voodoo that was, but it works...and automatically rewrote the url according to my permalink structure. So, idk if I've been missing something or if it was a combination of several things, but I'm not where I wanted to be. You are a saint...and I feel like an idiot :-) Dec 12, 2014 at 17:51
  • I elaborated a little on that and added this as a separate answer to your question.
    – tfrommen
    Dec 12, 2014 at 18:09
  • 1
    Would you mind accepting my answer to give this question some peace? You stated that it provides what you asked for in your comment.
    – tfrommen
    Dec 16, 2014 at 6:11

3 Answers 3

3

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
5
  • This template is useful if you want a specific term to be displayed different from other terms of the same taxonomy. But maybe I got the question wrong.
    – tfrommen
    Dec 12, 2014 at 17:35
  • I've tried that in a million different ways, and I still get nothing except a 404. I've refreshed my permalinks, but still nothing. I assume accessing the archive would be done by going to example.com/project/project-type/construction, right? Dec 12, 2014 at 17:43
  • And, the taxonomy template simply goes into the theme directory with all my other templates, correct? (Sounds stupid to ask, but just trying to rule anything out.) Dec 12, 2014 at 17:44
  • Yeah, that's right.
    – edwardr
    Dec 12, 2014 at 17:45
  • It depends on how you've configured the slug. Try opening the term list in the admin panel and clicking "view" to make sure you're on the right URL.
    – edwardr
    Dec 12, 2014 at 17:46
1

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>';
}
1
  • Which template type would this use? archive-custom_post_type.php or a taxonomy one?
    – Burgi
    Jan 30, 2018 at 17:51
1

I like to make a different Example for tfrommen's (correct) answer, because there is not a lot to find about this topic on the web.

  $post_type = get_post_type();

  if ( is_post_type_archive() ) {
    $taxonomies = get_object_taxonomies($post_type);
    $related_terms = get_terms(array(
      'taxonomy' => $taxonomies
    ));
  }

...

  if ( !empty($related_terms) && !is_wp_error($related_terms) ) {
    foreach ( $related_terms as $related_term ) {
      $term_url = get_term_link($related_term);

      if ( is_post_type_archive() ) {
        $term_url = add_query_arg( 'post_type', $post_type, $term_url );
      }
?>
  <a href="<?php echo esc_url($term_url); ?>"><?php echo $related_term->name; ?></a>
<?php    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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