0

I've got a custom post type defined to handle a company's distributors. It takes a post and routes it under /distributors/abc-truck-sales. I noticed that loading /distributors/ matches up to a category_name and is queried as such.

I've got a custom rewrite rule that is supposed to make /distributors/ into a redirection to the index.php

add_rewrite_rule('distributors/?$', 'index.php?show=all&post_type=distributor', 'top');

This rule is correctly added to the rewrite rules, and yet the rule '(.+?)/?$' => 'category_name=distributors', always matches first!

What do I have to do to prevent a category view? Unset it?

Update 1:

Changing the permalink category and tag bases accomplished nothing. Except prefix the paths, of course. If I load up /distributors/ it still assumes that it's a category (and I can change it to any old category and it'll load it).

1 Answer 1

1

The answer I've come up with on my own I know is a really bad idea. But it works and I have to have a working solution. (Doesn't mean I can't fix the problem later...)

add_filter('rewrite_rules_array', 'terminate_with_prejudice');
function terminate_with_prejudice($rules) {
  // if we don't remove this we'll never see our custom rule.
  unset($rules['(.+?)/?$']);

  $rules = array_merge(array(
    'distributors/?$' => 'index.php?show=all&post_type=distributor',
    '((?!distributor)([^/]*?))/?$' => 'index.php?category_name=$matches[1]',
  ), $rules);

  return $rules;
}

What this does is set the default rule to match against something that isn't "distributor" and has one section prior to the slash.

I just hope no other plugins/themes do this!

Edit:

This breaks the ability to view a page. Great.

I could really use some help on this, folks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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