Archive page with multiple taxonomies rewrite

Ok, I’ve read over pretty much every answer to this question, but my situation must be unique somehow.

What I’m attempting to do is straightforward in theory.

  • CPT: resource
  • Tax1: resource_type
  • Tax2: industry

When I register the CPT I have set the following:

'rewrite' => array(
        'slug' => 'resources/%resource_type%',
        'with_front' => false
    ),

'with_front' => false because the standard posts is using a base.

I also have a page of example.com/resources with a template of page-resources.php.

When I register Tax1 I have set the following:

'rewrite'  => array( 
        'slug' => 'resources',
        'with_front' => false
    )

I have a resource_type term named white-papers.

This produces my first desired outcome of a permalink that looks like
example.com/resources/white-papers/ for a taxonomy archive page using taxonomy-resource_type.php.

This is where things getting tricky. What I’m attempting to do is add a second taxonomy (Tax2) and create another archive page with a permalink of:

example.com/resources/white-papers/enterprise

This archive is a filtered archive of sorts in that it should show just the post that are of resource_type = white-papers AND industry = enterprise.

Instead I get 404.

But if I try to a URL with a query of example.com/index.php?post_type=resource&resource_type=white-papers&industry=enterprise I get my desired filtered archive page.

Also, and I’m not sure if its relevant to the issue or not, but I have this function running:

function resources_cpt_generating_rule($wp_rewrite) {
  $rules = array();
  $terms = get_terms( array(
    'taxonomy' => 'resource_type',
    'hide_empty' => false,
  ) );

$post_type="resource";
foreach ($terms as $term) {    

    $rules['resources/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=" . $post_type. "&resource_type=$matches[1]&name=$matches[1]';

}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'resources_cpt_generating_rule');

What I need help with is the rewrite rules for accomplishing this.

Any help is greatly appreciated in advance!

1 Answer
1

ok, so I thought I’d come back and answer my own question in case its useful to someone else.

To start, the CPT:

'query_var' => true,
    'has_archive' => false,
    'rewrite' => array(
        'slug' => 'resources',
        'with_front' => false
    ),

Tax1:

'query_vars' => true,
    'rewrite' => array( 
        'slug' => 'resources/type',
        'with_front' => false
    )

Tax2:

'query_vars' => true,
'rewrite' => array(
        'with_front' => false
    )

The rewrites:

add_rewrite_rule( '^resources/type/([^/]*)/industry/([^/]*)/?$', 'index.php?post_type=resources&resource_type=$matches[1]&industry=$matches[2]', 'top' );

This produces a slightly different URL structure than I originally intended, and while its a bit more verbose, I think it works just as well.

example.com/resources = page-resources.php

example.com/resources/type/white-papers = taxonomy-resource_type.php for Tax1 term white-papers.

example.com/resources/type/white-papers/industry/enterprise = the same taxonomy-resource_type.php archive template, but serves up only the CPT results of Tax1 AND Tax2.

This is exactly the functionality I was hoping to acheive.

Leave a Comment