Building an Archive Page for Custom Taxonomy in WordPress

WordPress provides a powerful framework for creating websites, and custom taxonomies play a crucial role in organizing content. When it comes to managing and displaying custom taxonomy archives, having a well-structured archive page becomes essential. In this article, we’ll guide you through the process of building an archive page for a custom taxonomy in WordPress.

Understanding Custom Taxonomies

Before diving into the details, let’s briefly review what custom taxonomies are in WordPress. Taxonomies are a way to classify and organize content. By default, WordPress comes with categories and tags, but sometimes you need more flexibility. Custom taxonomies allow you to create your own classification system, enhancing the way you organize and display content on your website.

Custom Taxonomy Archives in WordPress

Custom taxonomy archives function similarly to default taxonomy archives in WordPress. They share many similarities, including the use of the same template and loop structure.

The archive.php file can be used to render custom taxonomy archives effectively. This means that the same loop and overall structure that apply to default taxonomies are also applicable to custom taxonomies.

However, it’s important to note that taxonomies themselves do not have dedicated archive pages. For instance, attempting to access an archive page for a taxonomy like “Category” would result in a 404 error, as such a page does not exist. Instead, archive pages are available for specific terms within a taxonomy, not for the taxonomy as a whole.

http://localhost:8888/dosth/category 

Indeed, it wouldn’t be logical to attempt accessing a general category without specifying which particular category you’re interested in. In WordPress, what you actually access are the archive pages of specific terms within a taxonomy.

For example, consider this URL:

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

In this case, “advice” is a term within the “category” taxonomy, and the URL directs to its archive page.

Discussing a ‘Taxonomy Archive’ in WordPress context essentially means addressing the archive page of a term belonging to a particular taxonomy. To access any such taxonomy archive page, one would typically use a URL structured in a specific format.

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

For instance, to access the archive for a term like “google-play-store” in a custom taxonomy named “dosth_review_source”, you would use the following URL:

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

It’s important to note that if you have specified a “rewrite” argument when registering your taxonomy, and included a “slug” as a sub-argument, WordPress will use this slug in the URL for the archive, rather than the taxonomy ID. This approach is similar to what is commonly done for custom post types.

Accessing the archive of a custom taxonomy in WordPress requires using the slug specified in the rewrite argument, rather than the taxonomy ID. This changes the URL format to:

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

For example, in the case of the “dosth_review_source” custom taxonomy, if “review-source” is set as the slug, the archive URL becomes:

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

  • Initially, trying to access this URL may result in a 404 error. This happens because WordPress is not yet aware of the new “review-source” slug;
  • To rectify this, you need to flush the rewrite rules, allowing WordPress to recognize the change. This process is similar to what is done for custom post types;
  • To flush the rewrite rules, navigate to the Admin Dashboard, go to Settings, then Permalinks, and click on the “Save Changes” button at the bottom of the page. This action updates the permalink structure, making the new custom taxonomy slug recognizable by WordPress;
  • After updating the permalink settings and returning to the browser to refresh the URL http://localhost:8888/dosth/review-source/google-play-store, you might still encounter a 404 error. This issue persists regardless of how many times you flush the permalinks;
  • The reason for this persistent error lies in the configuration of the “dosth_reviews” post type. Specifically, it’s due to the $exclude_from_search argument being set to True during the registration of this post type;
  • The $exclude_from_search argument significantly influences the visibility of custom taxonomy archives. When it’s set to True, it prevents the associated posts from appearing in search results, which also impacts the taxonomy archives.

So, what’s the solution? You need to decide which feature is more critical: Is it more important to exclude reviews from search results, or is it more crucial to have a functioning custom taxonomy archive for “dosth_reviews”?

Based on typical requirements, having a custom taxonomy archive for the “dosth_reviews” post type tends to be more valuable in the long term. If there’s a need to hide reviews from search results, there are alternative methods to achieve this, which will be discussed in the next module.

Therefore, you should consider setting the $exclude_from_search argument to False or removing it entirely from the configuration array of the “dosth_reviews” post type.

Here’s an updated code snippet for registering the “dosth_reviews” post type without the $exclude_from_search argument:

$exclude_from_search argument:

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 revisit the URL for the custom taxonomy archive:

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

Hands typing on a laptop with code overlay graphics

Integrating Custom Taxonomy Archives into WordPress Menus

In WordPress, managing and accessing custom taxonomy archives can be streamlined through the use of the platform’s menu system. This approach eliminates the need to remember specific URLs for each custom taxonomy archive.

  • When creating a custom taxonomy, setting the $public argument to True enables the taxonomy terms to be visible and accessible in the WordPress Admin Dashboard, particularly on the Menus screen;
  • This visibility allows you to effortlessly add these taxonomy archives to any WordPress menu without the need to manually input their URLs, enhancing both convenience and efficiency in site navigation.

This approach, notable for its ease of use, extends beyond the realm of custom taxonomies. It is equally applicable to Custom Post Types, enabling a smooth incorporation into your website’s menu framework. Such a feature greatly streamlines the organization and retrieval of diverse content types on a WordPress site, enhancing overall site management.

Customizing “dosth_review_source” Taxonomy Archive Template

In order to customize the appearance of the taxonomy archive page for “dosth_review_source”, distinct from the standard blog layout, creating a specific template file is essential.

Delving into the WordPress Template Hierarchy, it’s evident that we need to follow a particular naming convention for this archive template file. This dedicated approach ensures that the “dosth_review_source” taxonomy archive has a unique design and functionality, setting it apart from the default post type archives.

taxonomy-{taxonomy_id}.php

Always consult the WordPress Template Hierarchy as your primary guide. 

In your code editor, create a new template file with the name:

taxonomy-dosth_review_source.php

Then, proceed to add the necessary code to this file. This will be the custom template specifically for the “dosth_review_source” taxonomy.

<?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(); ?>
A person with glasses with glasses focused on work at a computer

The code in taxonomy-dosth_review_source.php is essentially a replication of what’s in the archive-dosth_reviews.php file, with one key difference: a specific section of code has been completely removed from this version.

<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>

The removal of a certain section of code from the template is due to its redundancy; the Review Source is already being displayed as the primary headline of the archive. The taxonomy’s term archive title is being shown using a different method.

echo single_term_title();

This function is specifically tailored for custom taxonomy archives and does not apply to default archives like categories or tags.

The rest of the setup remains unchanged. The the_posts_pagination() function is also compatible with custom taxonomies, offering seamless integration.

For those seeking to modify the permalink structure of custom taxonomy archives, experimenting with the $rewrite argument is an option.  For a deeper understanding, consider reading the article at https://jonchristopher.us/blog/revisiting-custom-post-types-taxonomies-permalinks-slugs/ . Despite its age, this article remains relevant and informative.

To append additional text to the title of a taxonomy archive, it’s advisable to maintain simplicity in the approach.

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

Conclusion

Creating a custom post type taxonomy archive page in WordPress offers a flexible and efficient way to organize and display content. By utilizing custom templates, adjusting permalink structures with the $rewrite argument, and integrating pagination with the_posts_pagination() function, users can significantly enhance their website’s functionality and user experience. This approach not only streamlines content management but also allows for greater customization and specificity in how different types of content are presented and navigated, making it an essential skill for WordPress developers and content managers. You may also like the article about mastering WordPress Hooks for developers.

Leave a Reply