Streamlining Complex WordPress Taxonomies And Permalinks

The Problem of Overly Complex Taxonomies

As a WordPress site grows over time, it is common for the number and complexity of custom taxonomies to increase. Many sites start with just the default category and tag taxonomy, but as new types of content get added, additional custom taxonomies like “product_type”, “news_source”, or “book_genre” often get introduced. While this allows content to be classified in more ways, having too many taxonomies can create both organizational and performance issues:

  • The wp_term_taxonomy table that stores taxonomy terms can grow very large, slowing down queries
  • Front-end displays showing taxonomy filters and terms become cluttered as the number of taxonomies increases
  • Finding and filtering content across many taxonomies becomes more complex for administrators

For example, an online newspaper started with “category”, “post_tag”, and “newspaper_section” taxonomies. Over time they added additional taxonomies like “writer”, “geographic_focus”, “article_type”, etc. After a few years, they had over 50 different taxonomy terms that could be applied to articles. While this allows deep filtering and classification, it became unwieldy. Queries began slowing down, filter menus were confusing, and administering taxonomy terms became time-consuming with little added benefit.

Strategies for Consolidating Taxonomies

If your WordPress site is exhibiting signs of taxonomy bloat, taking the time to consolidate your taxonomy structure can help streamline site organization and simplify your database queries. Here are some best practices to follow when tackling this task:

Identifying which taxonomies can be merged or eliminated

Carefully audit your taxonomies looking for areas of overlap or redundancy. For example, you may be able to combine multiple types of product taxonomies (product_type, product_brand, product_line) into a single taxonomy using hierarchy to indicate type variations. Or you might find little usage for a rarely-used taxonomy like event_type which could be eliminated entirely.

Using hierarchical taxonomies effectively

Hierarchical taxonomies allow parent-child relationships between taxonomy terms. So you can potentially use a single taxonomy with nested hierarchy rather than having multiple simple taxonomies. For example, instead of separate actor, director, and producer taxonomies on a film site, you could have a single “film_crew” taxonomy with hierarchical terms like Actors > Lead Actors > Male Lead Actors.

Transitioning to a simplified taxonomy structure

When migrating or merging taxonomies, you’ll need to carefully port over any existing taxonomy terms and relationships assigned to content. Write a custom script to handle this, being cautious not to lose important metadata connections in the process. Notify your team of impending taxonomy changes and freeze further term creation in obsolete taxonomies that will be going away. And be sure to leave adequate redirect provisions for old taxonomy archives and URLs (covered more in the next section).

Implementing Permalink Changes Gracefully

Changing your taxonomy structure requires corresponding updates to permalinks to avoid dead links or lost traffic. Here are some best practices to handle this smoothly:

Understanding the role of permalinks in taxonomy management

Permalinks dictate the public URLs used to access post content archives organized by taxonomy, tag, category, author, etc. So as you change your taxonomy setup, you’ll need to migrate permalinks accordingly. For example, directing traffic from old category, tag, or custom taxonomy archives over to new consolidated hierarchies or structures.

Mapping existing permalinks to new taxonomy structure

Make a flowchart diagram mapping how existing category, tag, taxonomy archive URLs map to new categories, taxonomies, or term archives in your simplified structure. Identify redirect needs and account for variants like pagination. You may also need redirects for date, author, or search result archives that may be impacted.

Redirecting old permalinks to avoid dead links

Use 301 redirects in your .htaccess file to point old taxonomy and other archive URLs to appropriate new destinations. Redirects are preferable to dead links as they pass on link equity and avoid errors for users and bots. For example:

Redirect 301 /news/article_type/investigation /news/longform 

Redirects can also handle URL variants with regular expressions:

 
RedirectMatch 301 ^/news/([0-9]{4}/[0-9]{2}/.+)$ /archives/$1

Sample Code for Taxonomy and Permalink Improvements

When implementing taxonomy mergers, eliminations or hierarchy changes, you’ll need to make database changes in PHP and also handle redirects and front-end adjustments. Some examples:

PHP snippets for registering consolidated taxonomies

This registers a new unified “site_content” taxonomy to replace separate news, blog, video, etc taxonomies

register_taxonomy(
  'site_content', 
  ['post','page','video'],
  [
    'hierarchical' => true,
    'labels' => [...],
    'show_admin_column' => true,
    'rewrite' => [ 'slug' => 'content' ],
  ]
);

Apache redirects for handling old permalinks

301 redirect rules in .htaccess to send old taxonomy and date archives to new locations:

Redirect 301 /news/section/technology /content/technology
Redirect 301 /posts/([0-9]{4})/([0-9]{2})/?$ /archives?year=$1&month=$2

jQuery for updating front-end taxonomy filters

JavaScript to dynamically update site sidebar with consolidated “Types” taxonomy filter aftermerge:

jQuery(document).ready(function(){

  var typesList = "";

  jQuery.get("/wp-json/wp/v2/types", function(data){
    
    jQuery.each( data, function( key, val ) {
        typesList += "<li><a href='"+ val.link +"'>"+val.name+"</li>";
    });

    jQuery("#typeFilter").html(typesList);
  
  });

});

Tracking Progress and Handling Edge Cases

After simplifying your taxonomy structure and permalinks, continuing to monitor their effectiveness and handle any issues is important:

Using site analytics to monitor taxonomy usage

Keep an eye on your site analytics to see which consolidated taxonomies and their terms are getting good traffic versus little usage. You may find that further optimization or pruning is needed after observing real traffic and engagement data.

Fixing issues with misassigned taxonomy terms

It’s not uncommon to have stray edge cases of content that gets assigned incorrect taxonomy terms or disappears entirely during a migration. Have a plan for identifying and rectifying these stragglers, either manually or by writing custom queries.

Custom queries for retrieving legacy taxonomy content

In case you need to locate content marked with a legacy taxonomy that you eliminated, use WordPress’ rich custom query capabilities. For example, to find posts in the old “article_type” taxonomy:

$query = new WP_Query( array( 
  'tax_query' => array(
      array(
        'taxonomy' => 'article_type',
        'field' => 'slug',
        'terms' => 'investigation'
    )
  )
) );

Leave a Reply

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