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:

Read More

…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

Related posts

Leave a Reply

2 comments

  1. Recently I came across the same situation, and after a day spent searching for a solution and couldn’t find any, I wrote my own working solution.

    The trick is to use WordPress wp_rewrite, and to add your URLs to it when WordPress generates its permalinks on the beginning.

    I’ve wrote a simple function to do it all automatically and explained everything on my blog (don’t want to paste the same code twice), so feel free to check it out 😉

    http://someweblog.com/wordpress-custom-taxonomy-with-same-slug-as-custom-post-type/