Building archive page for custom taxonomy

Hire a WordPress Expert on Codeable
Updated On: May 10th, 2019 5 Comments

Custom taxonomy archives are no different from the default taxonomy archives. They both are the same in every aspect.

You can still use archive.php to render the custom taxonomy archive. Same Loop, Same everything.

When it comes to taxonomy archives, the taxonomy itself is not gonna have an archive.

For example, if you try to visit the following URL hoping to browse the archive of the “Category” taxonomy, you’ll be greeted with a 404, page not found error.

http://localhost:8888/dosth/category

Plus it doesn’t make any sense, right? What category are you trying to access?

When it comes to WordPress, terms belonging to a particular taxonomy will have an archive page. For example:

http://localhost:8888/dosth/category/advice

Whenever you hear the word Taxonomy Archive, it just means archive of a term which belongs to a particular taxonomy. You should be able to access any taxonomy archive page by entering the URL in the following format:

http://localhost:8888/dosth/{taxonomy_id}/{term-slug}

Using the above format, we can access our “dosth_review_source” taxonomy archive by entering the following URL:

http://localhost:8888/dosth/dosth_review_source/google-play-store

But when registering a taxonomy, if you have specified the “rewrite” argument with the “slug” as its sub-argument, WordPress will use the slug for the archive instead of taxonomy ID.

Just like how we did it for the custom post type, isn’t it?

So, you can no longer access the archive of the custom taxonomy by using its taxonomy ID, instead, you have to use the slug you have provided for the rewrite argument.

http://localhost:8888/dosth/{taxonomy-slug}/{term-slug}

In the case of our “dosth_review_source” custom taxonomy, we have provided “review-source” as the slug.

So, the archive URL is:

http://localhost:8888/dosth/review-source/google-play-store

If you now open up the above URL, it will result in 404 error. You already know the reason. WordPress doesn’t know about “review-source” slug yet!

So, we have to flush the rewrite rules for WordPress to take this change into effect! 

Just like how we did it for the custom post type, isn’t it?

So, go to Admin Dashboard -> Settings -> Permalinks

And click on the “Save Changes” button at the bottom of the page.

If you now go back to the browser and refresh the same URL, you’ll see the archive of testimonials belonging to “dosth_review_source” taxonomy.

http://localhost:8888/dosth/review-source/google-play-store

Ouch! Still 404 error! 

No matter how many time you flush permalinks, it is still going to be a 404 error!

This is happening because we have set the $exclude_from_search argument to True when we are registering “dosth_reviews” post type. 

The $exclude_from_search argument has a direct impact on the custom taxonomy archives.

“So, What do we do now?” 

Simple, it’s a trade off! Which feature is more important?

Is it excluding reviews from search results? or Is it custom taxonomy archive for our “dosth_reviews”?

According to my experience, the custom taxonomy archive for our “dosth_reviews” post type is more important in the long run. If the client doesn’t want reviews to show up in search results, there are alternative ways to deal with and we will be seeing them in the next module.

So, go ahead and set the $exclude_from_search to False or remove it altogether from the “dosth_reviews” configuration array.

Here is updated code for “dosth_reviews” post type:


register_post_type( 'dosth_reviews',
        array(
            'labels'  => array(
                'name'           => __( 'Reviews', 'nd_dosth' ),
                'singular_name'  => __( 'Review', 'nd_dosth' ),
                'add_new'        => __( 'Add Review', 'nd_dosth' ),
                'add_new_item'   => __( 'Add New Review', 'nd_dosth' ),
                'edit_item'      => __( 'Edit Review', 'nd_dosth' ),
                'all_items'      => __( 'All Reviews', 'nd_dosth' ),
                'not_found'      => __( 'No Reviews Found', 'nd_dosth' ),
            ),
            'menu_icon'             => 'dashicons-format-quote',
            'public'                => true,
            'exclude_from_search'   => false,
            'has_archive'           => true,
            'hierarchical'          => false,
            'show_in_rest'          => true,
            'rewrite'               => array( 'slug' => 'reviews' ),
            'supports'              => array( 'title', 'editor', 'custom-fields', 'thumbnail', 'excerpt', 'revisions', 'page-attributes' ),
            //'taxonomies'          => array( 'category', 'post_tag' )
        )
    );

If you now visit the custom taxonomy archive URL again:

http://localhost:8888/dosth/review-source/samsung-play-store

Ah! Finally! Neat, isn’t it?

You don’t have to remember the URLs

You can add the archives of a custom taxonomy to a WordPress menu. 

Because we set the $public argument to True while creating the taxonomy, we can access the taxonomy terms on the left-hand side of an individual menu in the Menus screen of the Admin Dashboard:

See? You don’t have to manually enter the URLs of your custom taxonomy archives. You can just add them to any WordPress menu. How convenient?

The same logic applies to Custom Post Types as well!

Let’s create the template file

Just like custom post type archive, We don’t want our taxonomy archive page to look like our blog.

So, let’s create a template file specific to the “dosth_review_source” taxonomy.

If you take a look at the WordPress Template Hierarchy,  It is clear that all we have to do is create an archive template file in the following naming format:

taxonomy-{taxonomy_id}.php

Remember, always keep WordPress Template Hierarchy as reference.

Alright, switch back to the code editor and create a template file called:

taxonomy-dosth_review_source.php

And put the following code in it:


<?php
/**
 * The template for displaying archive of Dosth Review Source taxonomy
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package Dosth
 */
get_header();
?>
<div class="content-container">
    <h1 class="page-title"><?php echo single_term_title(); ?></h1>    
    <div class="reviews-container">
        <div class="nd-dosth-reviews">
            <?php if ( have_posts() ): ?>
                <?php while( have_posts() ): ?>
                    <?php the_post(); ?>
                    <div class="review">
                        <blockquote>
                            <?php the_content(); ?>
                            <footer>
                                <cite><?php the_title(); ?></cite>
                            </footer>
                        </blockquote>
                    </div>
                <?php endwhile; ?>
                <?php the_posts_pagination(); ?>
            <?php else: ?>
                <p><?php _e( 'No Reviews found', 'nd_dosth' ); ?></p>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php get_footer(); ?>

Again! Nothing new in the above code. I copied the entire code from archive-dosth_reviews.php file and removed the following piece of code altogether:


<span class="review-from">
    <?php $terms = get_the_terms( get_the_ID() , 'dosth_review_source' ); ?>
    <?php printf( __( 'From %s', 'nd_dosth' ), $terms[0]->name ); ?>
</span>

We no longer need this. We already displaying the Review Source as the Primary Headline of the archive.

We are displaying the taxonomy’s term archive title with the help of:


echo single_term_title();

Note: This function only works the custom taxonomy archives. Not for the default archives like category, tag, etc.

That’s all. Everything else is the same.

The the_posts_pagination() the function will work for custom taxonomies too! Seriously, any custom taxonomy.

Here is the final output in the browser:

If you don’t really like the current Permalink structure for the custom taxonomy archive, you can play around with the $rewrite argument. Please read this article to know what I am talking about.

I know this is an age-old article. But it is still relevant and worth a read.

Finally, If you want to add some additional text to the title of the taxonomy archive, keep it simple like this:


<h1 class="page-title">
   <?php _e( 'Reviews From: ' ); ?><?php echo single_term_title(); ?>
</h1>

Ah! Finally! 

We are now done with custom post types and custom taxonomies. But there is still a lot to learn.

WordPress is an ocean in every aspect.

Don’t worry if you did not understand something. Mastering WordPress features take a lot of projects.

In the next lesson, we will create a plugin for hosting our custom post type and custom taxonomy registrations.

5 Replies to “Building archive page for custom taxonomy”

  1. Boris

    Thank you so much Naresh!! I forgot to update permalink structure and lost few hours searching why my custom tax is using index.php as template

    1. Naresh Devineni

      You’re welcome 🙂

  2. David

    Help a lot, thank you! Finally I don’t see 404 anymore 😀

  3. dan

    I am working on a listing website that uses a theme that produces custom post type with 2 Custom Taxonomy.

    Currently, users have 2 ways to reach a listing

    1 – Through the Listing-Location archive (Example – Listings in Texas)
    2 – Through the Listing-Category archive (Example – Listings of Plumbers)

    These listings appear on these archives because these options are available to be chosen when creating the listings.

    But I want to have a third archive.

    This archive will be like a bridge between the 2 established archives. This will be like Listing-Category in Listing-Location archive (Example – Listings of Plumbers in Texas)

    Every listing already has a Location selector (State and City) and a Category selector
    So I need a situation where any listing that matches both location and category will appear under this new “Category in Location” archive.

    Also I will like to display the links to these new archive taxonomy pages just as the existing category and Location links also display.

    So you think this is possible?

  4. Akk

    Thanks, brother

Leave a Reply

Your email address will not be published. Required fields are marked *