6

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
}
?>
8

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.

  • I'm getting a 404 with this after flushing the rewrite rules (just saving the permalinks in the back-end does this, right?). Not sure why... kopykatcopier.myweblinx.net/canon/copiers /brand/ and /type/ are both the correct slugs. Here's how I set up the CPT and taxonomies in my functions: pastebin.com/w7LBRABy – JacobTheDev Mar 12 '15 at 21:21
  • 2
    The URL should be yourdomain.com/brand/canon/type/copiers that is what you requested in the question and that is what I've handle in the answer. I've tested with categories and tags and the code works, so it should work for any other taxonomy pair. – cybmeta Mar 12 '15 at 21:28
  • 1
    Ah, alright. That's taking me to the home page for some reason. Hmm. – JacobTheDev Mar 12 '15 at 21:31
  • Yeah, I tested in your site and I get the home page. I've tested the code here with categoires and tags and the code works, so it should work with any other taxonomies pair. But as I said in the answer, you may need to fix several things, some of them may be related to conditional tags and that may be the reason of home page being displayed. For example, in my test with categories and tags, is_tag() falls when seeing the combination of category and tag. – cybmeta Mar 12 '15 at 21:36
  • 1
    As I said, you will face with several things to fix/work around. I can not cover all situations in one answer. Your problem with title is related to what I said about is_tax() falling when I combined categories and tags. You will need to check the query vars to detect when you are in an archive of both taxomies. I think it is better if you post a new question so we keep with one question per thread rule and keep this thread clean. – cybmeta Mar 14 '15 at 22:17
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!

  • This is great, thank you so much! Just one question: In the second rule, is the ? missing in the (.+) in user/(profession|city)/(.+)/?$? on purpose? – rassoh May 30 '18 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.