Removing taxonomy base using WP rewrite

I´m trying to create the following URL structure:

example.com/attractions                     <-- post type archive
example.com/attractions/taxonomy_term       <-- taxonomy archive
example.com/attractions/van-gogh-museum     <-- custom post type

Unfortunately this isn´t possible by default: WordPress needs the taxonomy base in the URL, as SE user sanchothefat wrote in his answer to this question:

…if you don’t want the taxonomy-slug to be there then you’re out of
luck, you can’t have the same slug as your post type or another
taxonomy as it won’t know what term is from which taxonomy and what’s
a post.

In other words, WordPress needs this permalink structure:

example.com/attractions/taxonomy_base/taxonomy_term

My question:
Is there a way to make this work using WordPress rewrite API?

I suppose it involves 2 steps:

  1. Removing the taxonomy base from the URL
  2. Tell WordPress what post type it needs to display

Update #1

I found a code snippet that should remove the taxonomy base from the URL:

global $wp_rewrite;
$wp_rewrite->extra_permastructs['taxonomy_base'] = array('%taxonomy_base%', EP_NONE);

(Source – see last answer on bottom of page)

This isn´t the complete solution though, since WordPress can´t tell if the post is a taxonomy archive or a post type…


Update #2

The author of this great article on Advanced Taxonomy Queries with Pretty URLs wrote me:

What you’re asking isn’t too difficult, but it could be a bit tricky.
The rewrite rules need to know what type of thing the second term
is(..) That’s why the category/tag/taxonomy name prefix is prepended.
You could create a rewrite so that everything after /attractions/ is
treated as a taxonomy URL, but doing both gets a little tricky.


Update #3

For anyone having the same issue, have a look at this discussion on WordPress Answers. Especially MikeSchinkel´s plugin code might help.

Here´s an interesting example of WPCandy combining the taxonomy base and term in the URL: http://wpcandy.com/pros/experienced/with-multisite

2 Answers
2

I just added a plugin to the WordPress Directory that removes the slug base for any custom taxonomy and the default category taxonomy. You can find it here – http://wordpress.org/extend/plugins/wp-no-taxonomy-base/

Leave a Comment