I have a (hopefully simple) question here.

Built in post type: Category

I have a custom post type: Rights, with a custom Category taxonomy for it (Rights Category).

My issue is using the rewrite slug of rights/category is being caught first by the generic category rewrite rule. So I am currently stuck using rights-category, or rights/categories (at least a little more elegant).

Is there any way (besides changing the Category base) to essentially say "Prioritize my custom rewrite rule over the built-in ones"? Last time I looked at Category's registration, I didn't think it was possible to register your own taxonomies before the built-in ones (priority 0), therefore placing your own rewrite rules ahead of WP Core's.

Note: I am defining the custom taxonomy and post type within mu-plugins. I also just tried using a priority of 0, and 1, and they seem to work with no repercussions. I'd still like some validation on this, however.

  • 1
    You can avoid conflict using a singular cpt name with plural archive slug: register_post_type('right', $args) where $args['has_archive'] = 'rights'. If you use plural cpt name only chance is add a custom rewrite rule, in that case see @cybnet answer. You can do registration for cpt, taxonomy and rewrite rule in same function, hooked on init (priority is not a important). – gmazzap Sep 17 '13 at 15:49
  • I think you misread the question. It's not the post type that is giving me the issue. It's the rewrite rules for Post Categories, that is conflicting with my Rights Categories. Basically, /category/* is being searched for before /rights/category/* is, in the order of the global $wp_rewrite. – Eric Holmes Sep 17 '13 at 16:00
  • 1
    Yes I know. But have you tried? I had, and with my tip it works. This is the code I tried and works in a MU plugin. After flushing rules, of course – gmazzap Sep 17 '13 at 16:02
  • @EricHolmes please add your code to your question. – Milo Sep 17 '13 at 16:11
  • @G.M. Exactly what I have (almost). The key to having this to work here is your init action is set to priority 1. So yes, this is correct, and what I mention above in my question. Want to move that to an answer so I can mark this complete? – Eric Holmes Sep 17 '13 at 17:52

I think that when you define custom rewrite slugs in the registration of a custom taxonomy, you still need to add custom rewrite rules.

function my_add_rewrite_rules() {
    add_rewrite_rule( 'rights/category/(.+)/page/([0-9]{1,})/?$', 'index.php?post_type=rights&category=$matches[1]&paged=$matches[2]', 'top' );
    add_rewrite_rule( 'rights/category/(.+)/?$' , 'index.php?post_type=rights&category=$matches[1]' , 'top' );

In order to add the correct rewrite rule I would need the code of the custom post type and custom taxonomy registration. Also, don't forget to flush rewrite rules by visiting WP admin area->settings->permalinks.

I can confirm from my own tests that @G.M. is correct (see first comment to the question). If you register your cpt with the singular name right and set the has_archive argument to the plurgal slug rights, the custom rewrite argument in register_taxonomy you are using will work without any custom rewrite rules. So you can register the cpt with register_post_type('right', $args) where $args['has_archive'] = 'rights'. But now you will have:

  • domain.com/rights/ -> for archive view
  • domain.com/right/a-custom-post/ -> for single custom post view
  • domain.com/rights/taxonomy-slug/term-slug/ -> the term archive view

So, I still prefer register the cutom post type with plural slug and add the rewrite rules in order to have a more consistent permalink structure like this (all urls will inclue 'rights' slug):

  • domain.com/rights/ -> for archive view
  • domain.com/rights/a-custom-post/ -> for single custom post view
  • domain.com/rights/taxonomy-slug/term-slug/ -> the term archive view
  • Shouldn't need a custom rewrite rule for a WordPress object (post type, taxonomy, etc.), but thanks for the answer! I'm also unsure that this would take precedence over the built-in category rewrite rules, unless you called this init action at 0, which is the same as I am doing above (working). – Eric Holmes Sep 17 '13 at 16:02
  • I suggested that because I had to do it for my own custom taxonomy. Without adding my own rewirte rules, the custom rewrite slug for a custom taxonomy was not working. Anyway, have you tried? The built-in category rewrite rules and the one suggested are differents. – cybmeta Sep 17 '13 at 16:11
  • The 'top' argument adds it to the top of the stack so it would match first if there's a clash... but there shouldn't be a clash since the pattern is unique from the built in category rules, and adding a custom rewrite rule should not be necessary. – Milo Sep 17 '13 at 16:41
  • This would work perfectly cybnet. But using the rewrite argument in register_taxonomy may save you some pain in the future. It seems using a priority of 0 on the init function will save you all the hassle :). – Eric Holmes Sep 17 '13 at 17:53
  • I think I didn't explain well or you didn't understand me. I'm not suggesting to use the custom rewrite rules or the rewrite argument in register_taxonomy, I'm suggesting to use both. The rewrite argument in register_taxonomy will create the permalinks, the custom rewrite rules will pass the correct query for that permalinks to Wordpress. Maybe, as @G.M. suggested, is this needed when a plural name is used?. I really don't know why or when this is needed but I needed in a custom project and it seems like you also need it. I've edited the answer to add support for pagination. – cybmeta Sep 17 '13 at 20:18

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.