Combining Multiple Taxonomies in one URL

The question:

I’m creating a complex site with a product catalog with two custom taxonomies. They are Brands and Types.

Brands
– Canon
– Toshiba
– HP

Types
– Printers
– Scanners
– Multifunction

I need a page that finds all products that are any combination of one brand and one type. The brand is the more important of the two, so it should be filtered as Brand -> Type (although it’d be ideal if this was interchangeable).

I can do this by modifying the query based off of a parameter I add to the end of the URL. For example, this URL could be one of the following:

But I’ve been told this isn’t great for SEO purposes. Ideally, I’d like the URL to be structured as http://www.example.com/brand/type/. This would result in URLs like:

Yes, I know I could set up sub-cateogires for each product type under each brand, but I also need to be able to view all products under each product type irrelevant of brand.

By the way, if the URL needs to be a little more complex (i.e. /brand/hp/type/multifunction/) that’s better than nothing.

Is there some trickery with permalinks I could do to get the links to be structured the way I need them to be?

UPDATE 10/23/15: I just spent an hour trying to diagnose why this stopped working on the site I had built. Turns out I was overriding the default query with a custom one on one of the taxonomy archive templates. To combat this, you can do the following:

<?php $tax_query = $wp_query->query_vars; ?>
<?php
if ($tax_query["types"] && $tax_query["brands"]) {
    // default query stuff here
} else {
    // custom query stuff here
}
?>

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

This rewrite rule should work (assuming “brand” and “type” are the taxonomy registered names):

function custom_rewrite_rules() {
    add_rewrite_rule('^brand/(.*)/type/(.*)?', 'index.php?brand=$matches[1]&type=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_rules');

Remember to flush the rewirte rules after saving this code in your site.

Then you will need to hook in several places to fix things. For example, you may need to hook wp_title to generate the correct title for the document, for example “HP Printers” instead or just “HP”, as this won’t be handle automatically by WordPress. I can’t give a exhaustive list of things you will need to fix.

Method 2

While digging up to answer this one, I found here the following rewrite function:

function eg_add_rewrite_rules() {
  global $wp_rewrite;
  $new_rules = array(
      'user/(profession|city)/(.+?)/(profession|city)/(.+?)/?$' => 'index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4),
      'user/(profession|city)/(.+)/?$' => 'index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2)
  );
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'eg_add_rewrite_rules' );

this function takes a url like: example.com/user/profession/designer,knight/city/athens,gotham/ and converts it in variables:

$proffession = "designer,knight";
$city = "athens,gotham";

which can be used in the taxonomy.php

More info: https://stackoverflow.com/a/34624138/5594521

Working demo: http://playground.georgemanousarides.com/

Hope it helps!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment